我在Azure PowerShell中创建了一个脚本。
如果我使用"echo"命令,它会将输出显示到控制台上。
但是,如果我使用Write-Output和Write-Error,则看不到输出。
我已将脚本“change-to-static.ps1”上传到存储帐户。然后我使用顶部栏上的按钮打开“Cloud Shell”。然后在PowerShell控制台中键入“./change-ip-to-static.ps1”。
因此,除非我将“Write-Output”和“Write-Error”替换为“echo”或“print”,否则该脚本不会产生任何输出。
请帮帮我。我该怎么做才能看到输出?
以下是脚本内容。
类似问题请参见如何在PowerShell中输出内容。我已经阅读过了,但是没有具体的示例说明如何实现我的目标,即如何修改我的脚本以查看输出。并且在我的情况下,即使我重定向到文本文件,它也不会输出。但是,在我的情况下,“echo”和“print”等命令可以工作,但它们在上面的示例中没有涉及。请参见以下脚本。
$IPs = Get-AzPublicIpAddress;
$Static = "Static";
foreach ($PublicIP in $IPs) {
$Method = $PublicIP.PublicIpAllocationMethod;
$Name = $PublicIP.Name;
if ($Method -eq $Static) {
$message = "The method of " + $Name + " is already " + $Static;
Write-Progress -Activity $message;
}
else {
Write-Progress -Activity "Changing the method of "+$Name+" from "+$Method+" to "+$Static+"...";
$PublicIP.PublicIpAllocationMethod = $Static;
Set-AzPublicIpAddress -PublicIpAddress $PublicIP;
Write-Progress -Activity "Querying the method of "+$Name+"...";
$ModifiedAddress = Get-AzPublicIpAddress -Name $Name -ResourceGroupName $PublicIP.ResourceGroupName -Location $PublicIP.Location
$NewMethod = $ModifiedAddress.PublicIpAllocationMethod;
if ($NewMethod -eq $Static) {
Write-Output "The method for "+$Name+" has successfully changed to "+$Static;
}
else {
Write-Error -Message "Cannot change the method for "+$Name+" to "+$Static+", it is still "+$NewMethod+"!!!";
}
}
}
附注:我已根据建议更新了脚本(使用此网址),但仍没有输出。只有“echo”或“print”会输出结果。
另外,Write-Progress
在执行需要几秒钟才能完成的 Set-AzPublicIpAddress
或添加 Start-Sleep
命令时甚至不显示临时消息。它仅在执行 Get-AzPublicIpAddress
时设置。