gpt4 book ai didi

powershell - 使用 Powershell 使用 Azure VM 创建自动关闭策略

转载 作者:行者123 更新时间:2023-12-02 23:09:01 24 4
gpt4 key购买 nike

我尝试使用 Powershell 为我的 Azure VM 创建自动关闭策略,但不断遇到此错误:

New-AzureRmResource : MissingRequiredProperty : Missing required property TargetResourceId. At 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

最佳答案

原因:

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

尝试使用以下 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

结果如下:

enter image description here

关于powershell - 使用 Powershell 使用 Azure VM 创建自动关闭策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49126320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com