Introduction I introduced how to set up the UCSM automation lab and used PowerTool, the Cisco PowerShell module, to connect and manage UCSM (see Power
I introduced how to set up the UCSM automation lab and used PowerTool, the Cisco PowerShell module, to connect and manage UCSM (see PowerShell to Automate UCS (1): Lab Setup).
This blog is to introduce my favourite PowerShell editor, PowerGUI, which provides an intuitive graphic interface, simplifies scripting work and allows easy test and troubleshooting. Most importantly it is free!
Download PowerGUI from DELL (It is now owned by DELL): http://software.dell.com/products/powergui-freeware/
PowerGUI 3.8 is used in this lab. The lab steps are as below:
Step 1: Install PowerGUI on Windows
Nothing fancy, just follow the installation wizard and click though.
Step 2: PowerGUI Administrative Console
After successfully install PowerGUI, two programs are installed ‘PowerGUI Administrative Console‘ and ‘PowerGUI Script Editor‘. I am not going to elaborate ‘PowerGUI Administrator Console’ in this blog. However, if you need to add a PowerShell library such as AD snap-in to manage Active Directory (get-aduser, adgroup etc. commands are unavailable if not install the library), you will need to use ‘PowerGUI Administrative Console’ as below and add through ‘File > PowerShell Libraries…’.
Step 3: PowerGUI Script Editor
‘PowerGUI Script Editor’ is the tool we are using to write PowerShell script. The window should be as below:
1 – Create new script
2 – Script browser
3 – Script editing area
# starts comments, and is shown in Green in the editor.
Command is shown in Aqua such as ‘import-module’. PowerGUI also provides command hint as below. Move mouse pointer over a command, it will reveal the command syntax.
Variables are in purple. After executing the script once, variable values are shown in ‘4 Variables’, which I found useful in troubleshooting whether values are parsed as required.
4 – Variables used in script
This area shows default and user-defined variables and the value stored. Arrange is also supported in PowerShell, we will come to it in the next blog. Variable type is not normally required in PowerShell; though it can be define via [string] [int] and etc. in front of a variable. For example,the following is a string:
[string]$var = 5
$ to indicate a variable is followed; = to assign value to variable; “” to indicate string as value; while numerical value is assgined without “”. For example, the first one is a string; the second is an integer.
$password = "ucspe"
$number = 5
5 – Execute script
The result will be shown in ‘7 – Script execution result’.
6 – Script execution status
Whether the execution is completed.
7 – Script execution result
If the script completes successfully, the result will show here; if the script fails to run, debug information will show. Our result from ‘get-ucsstatus’ is as below:
We can also select certain attribute instead of retrieve everything using ‘get-ucsstatus’. We now change the command to:
get-ucsstatus | select VirtualIpv4Address
The result will be as below:
Is it similar to SQL syntax? Yes, and PowerShell support ‘where’ to define condition as well!
Today’s PowerTool Script
Today’s full script is as below:
<span style="color:#008000;">#clear screen.</span>
<span style="color:#008000;">#Import Cisco UCS PowerTool module into PowerShell.</span>
<span style="color:#008000;">#Define variables.</span>
<span style="color:#008000;">#Auto logon UCSM, credentials are not manually input into the pop-up window anymore (see <a href="https://autrunk.wordpress.com/2016/06/26/powershell-to-automate-ucs-1-lab-setup/">PowerShell to Automate UCS (1): Lab Setup</a>).</span>
$user = "ucspe"
$password = "ucspe" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object system.Management.Automation.PSCredential ($user,$password)
Connect-Ucs $ucsvip -Credential $cred
<span style="color:#008000;">#Retrieve the current UCS stauts to verify the logon was successful.</span>
<span style="color:#008000;">#Disconnect the connection to UCSM.</span>
To be continued
In today’s lab, PowerGUI is used as PowerShell editor to write and test scripts. The script connects to the UCSM built in PowerShell to Automate UCS (1): Lab Setup and retrieves UCSM status.
Future blogs will introduce more useful scripts and tricks.
Learning by doing; doing by learning…reading and thinking alone won’t make fast learner, that’s why I am writing these lab blogs.