I have created a script in the Azure PowerShell.
If I use the "echo" command, it displays output to the console.
However, if I use Write-Output and Write-Error, I don't see the output.
I have uploaded the script "change-to-static.ps1" to a storage account. Then I open the "Cloud Shell" with a button at the top bar. Then I type "./change-ip-to-static.ps1" in the PowerShell console.
Therefore, the script does not produce any output unless I replace "Write-Output" and "Write-Error" with "echo" or "print".
Please help me. What should I do to see the output?
The script is below.
There is a similar question at How to output something in PowerShell. I have read it, but there are no concrete examples on how to achieve my goal, i.e. how to modify my script to see the output. And in my case, it does not output even if I redirect to a text file. However, commands like "echo" and "print" in my case work but they are not covered in the above example. See the script below.
$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+"!!!";
}
}
}
P.S. I have updated the script (use this URL) according to the suggestions, but there is still no output. Only "echo" or "print" gives the output.
P.P.S. The Write-Progress does not even show a temporary message in the status line during Set-AzPublicIpAddress which takes a couple of seconds to complete, or if I add the Start-Sleep cmdlet. It does only set during Get-AzPublicIpAddress.
After reading your last edit to my answer, I believe you made a bit of confusion in using
Write-*commandlets, and in your script logic, so I provided a more detailed answer with context.echoin the Powershell Azure Cloud Shell is an alias ofWrite-Output, as executingechowithout parameters clearly shows (docs here ).Moreover: the unix
echocan also be run in the Powershell Azure Cloud Shell.print, on the other hand, is not a Powershell alias, so the unix counterpart is the one which always get executed when using theprintkeyword (presently a symlink torun-mailcap- but it's not clear to me how it comes into play into your use case.)So, basically,
echoandWrite-Outputwill both work, because they call the same commandlet, unless you execute/usr/bin/echodirectly, mixing up technologies and effectively impairing portability.Back to the question:
Write-Outputworks as expected. The logic is faulty: You use=as a comparison operator, but you need to use-eqinstead.Write-Progressneeds to be used differently, replace it withWrite-HostorWrite-Output. Refer to the docs for an explanation.Note that
Write-Outputsends an object down the pipeline, which can eventually be represented as a console output.Write-ProgressandWrite-Host, on the other hand, do not generate output - the latter sends an object to the host for displaying, though, soWrite-Hostis the recommended way to display something in the console. Refer to this question for more details onWrite-Host,Write-Outputand the Powershell pipeline.