Windows Azure - Cmdlets Usage

In the previous article we have explored the Installation of:

  • Windows Azure Platform PowerShell Cmdlets

You can refer the article here.

For continuing with this article you need to open the PowerShell window and enter the command: Add-PSSnapin WAPPSCmdlets

Cmdlets

Below are the list of Cmdlets and their usage.

  1. Getting List of Cmdlets

    For getting the list of Cmdlet in the loaded package, use Get-Command
    E.g.: Get-Command -Module WAPPSCmdlets

    WinAzrCMD1.gif
     
  2. Getting Help

    For getting help on a command use Get-Help command followed by the Cmdlet name
    E.g.: Get-Help New-Deployment

    WinAzrCMD2.gif
     
  3. Gettting Help with Example

    For getting help with example on a particular command use the –examples option
    E.g.: Get-Help New-Deployment –examples

    WinAzrCMD3.gif

Like -examples, there are options –detailed and –full to get detailed and full help on the topic.

Note: You can also get the common commands like:

  • exit - To exit the PowerShell application
  • cls - To clear the PowerShell screen
  • echo - To display text on the screen

Creating PowerShell Scripts

We can create PowerShell Script files. The default extension should be .ps1.

Place the following content inside a file named Command.ps1

Echo "Hello, World!"

WinAzrCMD4.gif

Editing PowerShell Scripts

We can edit the PowerShell script file using the Edit option as shown below.

WinAzrCMD5.gif

On clicking the Edit option you can see the following PowerShell Editor.

WinAzrCMD6.gif

On executing the script you will get the following error:

"File cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details."

Reason: The system settings restricts running PowerShell scripts.

Solution: Open the PowerShell window in Administrator mode and run the following command:

Set-ExecutionPolicy Unrestricted

Note: The Execution Policy supports Unrestricted, Signed and Remotely Signed scripts. This ensures that an unauthorized script is not executed in the system. Making policy to Unrestricted is not recommended by for learning purposes it should be fine.

WinAzrCMD7.gif

If you do not run in Administrator mode, you will get the following error:

Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft .PowerShell' is denied.

Reason: The Local Machine registry key is being shared by all users of system. So Administrative privilege is needed to modify it.
Solution: Run the PowerShell in Administrator mode

WinAzrCMD8.gif

After the above problems are resolved, you can see the following output from Editor.

WinAzrCMD9.gif

Running PowerShell Command Script

In case you need to execute the script manually or through application, you can use the following command.

powershell.exe –noexit c:\temp\command.ps1

The option –noexit says to make the execution window remained to view the results. Run the following command inside the Windows Run window and you can see the following result.

WinAzrCMD10.gif

We can create a windows command script (.cmd) using the above command and later we executing the same with just a double click.

Invoking a Cmdlet

We are now ready to test a real example of invoking a cmdlet.

Cmdlet: Get-HostedServices piped with Get-HostedProperties
Inputs: SubscriptionId, CertificateThumbprint

WinAzrCMD11.gif

The first pane shows the script and the second pane shows the output.

In the script pane the first two lines are variables declaration and value assign. The Certificate thumbprint can be obtained from Windows Azure Portal. (Management Certificates > Subscriptions > Select the first child node > Thumbprint property)

In the above script the output of Get-HostedServices is piped with Get-HostedProperties.

This concludes the usage of Cmdlets. More Cmdlet can be experiment through the Get-Help option. You can find more examples here.

Summary

In this article we have experimented on:

  • Getting Help of Cmdlet
  • Getting Help with Examples of Cmdlet
  • Creating PowerShell Command Script
  • Using PowerShell Script Editor
  • Resolving Initial Errors in PowerShell
  • Using Cmdlets to get log

I hope this information is enough for going ahead and automate Azure tasks using the PowerShell Cmdlet and editor.