简体   繁体   中英

Azure Powershell Output not displayed in the console

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, ie 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+"!!!";
        }
    }
}

PS 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. PPS 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.

echo in the Powershell Azure Cloud Shell is an alias of Write-Output , as executing echo without parameters clearly shows (docs here ).

PS /home/mikelangelo> echo

cmdlet Write-Output at command pipeline position 1
Supply values for the following parameters:
InputObject:

Moreover: the unix echo can also be run in the Powershell Azure Cloud Shell.

PS /home/mikelangelo> which echo
/usr/bin/echo 
PS /home/mikelangelo> /usr/bin/echo ciao mondo
ciao mondo

print , on the other hand, is not a Powershell alias, so the unix counterpart is the one which always get executed when using the print keyword (presently a symlink to run-mailcap - but it's not clear to me how it comes into play into your use case.)

PS /home/mikelangelo> which print
/usr/bin/print

So, basically, echo and Write-Output will both work, because they call the same commandlet, unless you execute /usr/bin/echo directly, mixing up technologies and effectively impairing portability.

Back to the question: Write-Output works as expected. The logic is faulty: You use = as a comparison operator, but you need to use -eq instead.

Write-Progress needs to be used differently, replace it with Write-Host or Write-Output . Refer to the docs for an explanation.

Note that Write-Output sends an object down the pipeline, which can eventually be represented as a console output. Write-Progress and Write-Host , on the other hand, do not generate output - the latter sends an object to the host for displaying, though, so Write-Host is the recommended way to display something in the console. Refer to this question for more details on Write-Host , Write-Output and the Powershell pipeline.

Like other commenters before me I can also confirm that the code from your gist works just fine in Azure Cloud Shell.

I noticed that there is only an output if you have at least one dynamic public ip that the script can change to static. The reason is, that only in this case you use Write-Output to return a string to the console.

If there is no dynamic public ip left, your script only writes a progress message, but you never get to see it, as the script execution ends too quickly after you write the message and progress messages don't linger.

Put the command Start-Sleep -Seconds 2 under the line with Write-Progress and you will see what I mean:

$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;
        Start-Sleep -Seconds 2 # This will keep the script running 2 seconds longer and the message visible.
    }
    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
        $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!!!";
        }
    }
}

Write-Progress is probably not the cmdlet that you want to use to write out the progress of your script (despite it's name). As I do not see how you would need to further process the output of your script, you might as well replace it with Write-Host .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM