使用带有Powershell的Azure VM创建AutoShutdown策略

时间:2018-03-06 08:27:26

标签: powershell azure virtual-machine

我正在尝试使用Powershell为Azure VM创建自动关闭策略,但仍然遇到此错误:

  

New-AzureRmResource:MissingRequiredProperty:缺少必需属性TargetResourceId。   在C:\ Users \ home \ Documents \ CreateAzureVM.ps1:167 char:1   + New-AzureRmResource -Location $ Loc -ResourceId $ ScheduledShutdownReso ...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:CloseError:(:) [New-AzureRmResource],ErrorResponseMessageException       + FullyQualifiedErrorId:MissingRequiredProperty,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceCmdlet

我对如何修复此错误感到迷茫,这是我目前的脚本:

 $SubscriptionId = $AzContext.Context.Subscription.Id;  
$VMResourceId = (Get-AzureRmVM).id
$ScheduledShutdownResourceId = "/subscriptions/$SubscriptionId/resourceGroups/$RSGName/providers/microsoft.devtestlab/schedules/shutdown-computevm-$VMName"

$Properties = @{}
$Properties.Add('status', 'Enabled')
$Properties.Add('taskType', 'ComputeVmShutdownTask')
$Properties.Add('dailyRecurrence', @{'time'= 1159})
$Properties.Add('timeZoneId', "Eastern Standard Time")
$Properties.Add('notificationSettings', @{status='Disabled'; timeInMinutes=15})
$Properties.Add('targetResourceId', $VMResourceId)

#Error
New-AzureRmResource -Location $Loc -ResourceId $ScheduledShutdownResourceId -Properties $Properties -Force

2 个答案:

答案 0 :(得分:3)

原因:

此脚本$SubscriptionId = $AzContext.Context.Subscription.Id $VM = Get-AzureRmVM -ResourceGroupName $RGName -Name VMName $VMResourceId = $VM.Id $ScheduledShutdownResourceId = "/subscriptions/$SubscriptionId/resourceGroups/wayneVMRG/providers/microsoft.devtestlab/schedules/shutdown-computevm-$VMName" $Properties = @{} $Properties.Add('status', 'Enabled') $Properties.Add('taskType', 'ComputeVmShutdownTask') $Properties.Add('dailyRecurrence', @{'time'= 1159}) $Properties.Add('timeZoneId', "Eastern Standard Time") $Properties.Add('notificationSettings', @{status='Disabled'; timeInMinutes=15}) $Properties.Add('targetResourceId', $VMResourceId) #Error New-AzureRmResource -Location eastus -ResourceId $ScheduledShutdownResourceId -Properties $Properties -Force 不适用于特定VM。你应该得到一个特定的虚拟机。

尝试使用以下Powershell脚本:

req.file('file').upload({
  adapter : require('skipper-gridfs'),
  uri : 'mongodb://192.168.99.100:32769/admin/upload'
})

结果如下:

enter image description here

答案 1 :(得分:0)

这里是一个循环,用于读取AZ虚拟机的自动关闭的当前配置值(可以根据上面的Wayne Yang示例轻松添加更新/更改/设置) 示例将通过许多订阅循环。 :

###################
##:List all subs which are enabled
#$AllSubID = (Get-AzureRmSubscription | Where {$_.State -eq "enabled"}).SubscriptionId
### above might not work depends on account, just get all below.
$AllSubID = (Get-AzureRmSubscription).SubscriptionId
Write-Output "$(Get-Date -format s) :: List of Subscription below"
$AllSubID


$AllVMList = @()
Foreach ($SubID in $AllSubID) {
Select-AzureRmSubscription -Subscriptionid "$SubID"

##list all VMs
$VMs = Get-AzureRmVM
Foreach ($VM in $VMs) {
    $VM = New-Object psobject -Property @{`
        "Subscriptionid" = $SubID;
        "ResourceGroupName" = $VM.ResourceGroupName;
        "VMName" = $VM.Name}
        $AllVMList += $VM | select Subscriptionid,ResourceGroupName,VMName
        }
}

$AllVMList

## Get AutoShutdown info
Foreach ($VM in $AllVMList) {
    Write-Output "$(Get-Date -format s) :: VM: $($VM.VMName) :: $($VM.ResourceGroupName) :: $($VM.Subscriptionid)"
    $ScheduledShutdownResourceId = "/subscriptions/$($VM.Subscriptionid)/resourceGroups/$($VM.ResourceGroupName)/providers/microsoft.devtestlab/schedules/shutdown-computevm-$($VM.VMName)"
    ## Write-Output "$ScheduledShutdownResourceId"
    $VMShutdownInfo = get-AzureRmResource -ResourceId $ScheduledShutdownResourceId
    Write-Output "$(Get-Date -format s) :: VM: $($VM.VMName) :: status: $($VMShutdownInfo.properties.status) ; taskType: $($VMShutdownInfo.properties.taskType) ; timeZoneId: $($VMShutdownInfo.properties.timeZoneId) ; dailyRecurrence: $($VMShutdownInfo.properties.dailyRecurrence) ;  "
}
###Done