首页 > 解决方案 > 我正在尝试在 azure cloud 的 powershell 脚本中添加异常处理,但是当错误发生时尝试块没有响应

问题描述

#adding wrong parameters for checking the try block
$RGName = "RG1"
$VMName="VM1"
$VMSize="standard_B1ls"

$VirtualMachine = Get-AzVm | Where-Object {$_.ResourceGroupName -eq $RGName } | Where-Object {$_.Name -eq $VMName}
$virtualMachine.HardwareProfile.VmSize = $VMSize 
#try block       
    try{
        Update-AzVM -VM $VirtualMachine -ResourceGroupName $RGname
        write-host "you are in try block "
    }
    catch{
        write-host "you are in catch block "
    }

catch 块不起作用我不需要打印 try 块 write-host 发生异常而不是 print catch write-host

标签: azurepowershellazure-powershell

解决方案


推荐阅读