我有下面的代码,它将检查是否存在网络对等并采取相应的行动,我最近更改了二头肌管道以添加
failOnStandardError: true
,因为我注意到有时二头肌代码会失败,但管道给人的印象是它运行了成功了。
$peering = az network vnet peering show -g xxx -n enter-wrong-peering--vnet-name vnet_name --query "name" --output tsv
这会返回错误。
ERROR: (NotFound) Resource /subscriptions/xx/resourceGroups/xxx/providers/Microsoft.Network/virtualNetworks/xxx/virtualNetworkPeerings/xxx not found. Code: NotFound Message: Resource /subscriptions/xxx/resourceGroups/xx/providers/Microsoft.Network/virtualNetworks/xxx/xxx not found.
我的问题是,有没有办法让下面的这部分代码静音?
$ErrorActionPreference = "SilentlyContinue"
try {
$peering = az network vnet peering show -g xxx -n enter-wrong-peering--vnet-name vnet_name --query "name" --output tsv
}
catch {
Write-Output "Peering does not exist"
}
我尝试将代码放在 try/catch 块中并将 Errorcontrol 设置为
SilentlyContinue
您是否尝试过重定向 stderr 输出?
$peering = az network vnet peering show -g xxx -n enter-wrong-peering--vnet-name vnet_name --query "name" --output tsv 2>error.txt