【问题标题】:Creating AutoShutdown Policy with Azure VM with Powershell使用带有 Powershell 的 Azure VM 创建自动关机策略
【发布时间】:2018-03-06 08:27:26
【问题描述】:

我正在尝试使用 Powershell 为我的 Azure VM 创建自动关机策略,但一直遇到此错误:

New-AzureRmResource:MissingRequiredProperty:缺少必需的属性 TargetResourceId。 在 C:\Users\home\Documents\CreateAzureVM.ps1:167 char:1 + 新 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

【问题讨论】:

    标签: powershell azure virtual-machine


    【解决方案1】:

    原因:

    此脚本$VMResourceId = (Get-AzureRmVM).id 不适用于特定的虚拟机。你应该得到一个特定的虚拟机。

    尝试使用以下 Powershell 脚本:

    $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
    

    结果如下:

    【讨论】:

      【解决方案2】:

      这里有一个循环来读取当前配置的 AZ vms 自动关闭值(可以根据上面的 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
      

      【讨论】:

        猜你喜欢
        • 2020-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-22
        • 1970-01-01
        • 1970-01-01
        • 2021-01-16
        相关资源
        最近更新 更多