Introduction PowerShell to Automate UCS (1): Lab Setup introduced the UCSM and PowerTool lab setup. PowerShell to Automate UCS (2): PowerGUI Editor i
PowerShell to Automate UCS (1): Lab Setup introduced the UCSM and PowerTool lab setup.
PowerShell to Automate UCS (2): PowerGUI Editor introduced how to use PowerGUI as PowerShell script editor to simplify script development test; and also script example.
This blog is to demonstrate how to develop PowerShell script to manage UCSM if you are new to PowerShell and have UCSM configuration experience.
Cisco PowerTool provides the function to convert UCSM GUI to PowerShell commands. Basically we just need to configure an example from UCSM GUI interface, the actions will be captured and output as PowerShell commands in ‘Cisco UCS Manager PowerTool’ application window.
The auto script generation works fine most of time, though it may provide too many attributes than necessary. After gaining some experience with PowerShell/PowerTool, it should be pretty easy to tailor the generated script. It seems Cisco finally provides some kinda of user-friendly product…I think Cisco try to make things difficult to protect its profitable certificate system ;p
Step 1: Launch Cisco UCS Manager PowerTool
Lab setup and installation see PowerShell to Automate UCS (1): Lab Setup
After launch Cisco UCS Manager PowerTool, the following window appears. It says ‘To generate PowerShell code from a running UCS Manager GUI: ConvertTo-UcsCmdlet’. However, it won’t work by directly typing in ‘ConvertTo-UcsCmdlet’, the rest steps need to be followed through.
Step 2: Connect to UCSM by Running ‘Connect-Ucs’
Connect to UCSM by running Connect-Ucs <UCSM-ipaddress> . See PowerShell to Automate UCS (1): Lab Setup for details.
Step 3: Launch UCSM GUI by Running ‘Start-UcsGuiSession -LogAllXml’
After successfully log into UCSM GUI, run ‘start-ucsguisession -logallxml’ to launch UCSM GUI. Please note Java is required to open the GUI; HTTP GUI cannot be started from the command line. Following screenshot shows the Java-version of GUI is started.
Step 4: Run ‘ConvertTo-UcsCmdlet’ from PowerTool
After UCSM GUI launched, run ‘ConvertTo-UcsCmdlet’ in the PowerTool window.
Step 5: Perform configuration in UCSM GUI and PowerTool displays PowerShell code
Perform required configuration in UCSM GUI as normal and the PowerTool window will displays the output in PowerShell code as below:
The example is to create two MAC pool, FI-A side and FI-B side each, and assign mac address blocks respectively.
First, I create the required MAC pools from UCSM GUI and use ‘ConvertTo-UcsCmdlet’ function.The MAC pools are configured as below from the GUI.
The auto-generated PS script is as below:
Based on the auto-generated PS codes, I modified the script developed in PowerShell to Automate UCS (2): PowerGUI Editor as below:
<span style="color:#008000;">#clear screen</span>
<span style="color:#008000;">#Import Cisco UCS PowerTool Module in PowerShell</span>
#Input variables are configured here</span>
<span style="color:#008000;">#Auto logon UCSM, credentials are not manually input in pop-up window</span>
$user = "ucspe"
$password = "ucspe" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object system.Management.Automation.PSCredential ($user,$password)
Connect-Ucs $ucsvip -Credential $cred
#Create MAC Pool A and assign MAC address block</span>
$mo = Get-UcsOrg -Level root | Add-UcsMacPool -Descr "FI-A side Mac Pool" -Name "MAC_A"
$mo_1 = $mo | Add-UcsMacMemberBlock -From "00:25:B5:1A:00:00" -To "00:25:B5:1A:00:20"
<span style="color:#008000;">#Create MAC Pool B and assign MAC address block</span>
$mo = Get-UcsOrg -level root | Add-UcsMacPool -Descr "FI-B side Mac Pool" -Name "MAC_B"
$mo_1 = $mo | Add-UcsMacMemberBlock -From "00:25:B5:1B:00:00" -To "00:25:B5:1B:00:20"
#Disconnect the connection to UCSM</span>
‘Start-UcsTransaction’ and ‘Complete-UcsTransaction’ pair separate code section; $mo and $mo_1 are local variables to the section. We don’t have to come up with different variable names in this case. ‘get’ means query/retrieve in PowerShell; ‘add’ means add new configuration; ‘remove’ means remove/delete the configuration.
‘|’ pipe means the previous command’s output as input to the following command. For example, we retrieve ‘root’ location by executing ‘get-ucsorg -level root’ and then add MAC pool to the location; we retrieve the created MAC pool, and then add MAC address block to the MAC pool.
Before running the PowerShell script, I deleted the manually created MAC pools. The following PS codes are auto generated; where you can see the command to remove/delete MAC pool.
Run the script and open UCSM GUI to check the result. The following screenshot shows two MAC pools are created with required MAC address blocks.
Here comes today’s home work. How to use PowerShell to create vNIC template in UCSM?
Hints are as below. Although I didn’t use auto-generation, please use PS code auto-generation, run the auto-generated script and observe results from UCSM GUI.
<span style="color:#008000;">#Create vNIC template. ModifyPresent allows overwrite present configuration - remove or add based on requirement</span>
Add-UcsVnicTemplate -Name $ucsm.vNIC_Name1 -IdentPoolName $ucsm.vNIC_MAC1 -MTU $ucsm.vNIC_MTU1 -NwCtrlPolicyName $ucsm.vNIC_NtwCtrl1 -SwitchId $ucsm.vNIC_SwitchId1 -TemplType $ucsm.vNIC_TemplType1 -QosPolicyName $ucsm.vNIC_QoS1 -ModifyPresent
<span style="color:#008000;">#Add vlan to vNIC template and set default vlan</span>
Add-UcsVnicInterface -VnicTemplate $ucsm.vNIC_Name1 -Name iscsi_a -DefaultNet yes
To be continued
In today’s lab, we used PowerTool to auto generate PS codes and developed a PS script to create MAC pools.
Script may seem time consuming if you only have to configure one UCSM in life…however, if the client has multiple UCS blade chassis, or have to deploy for multiple clients, it will turn out to be time saving.
Moreover, if we are talking about operation, the automation allows user with little UCSM experience to deploy additional servers in pattern – no need to understand how UCSM works but only provide a few variable values.
Cisco UCS Director can also be integrated with PowerTool, which provides user a service catalogue interface to request server, provide variable values, and the server will be automatically provisioned.
Alternatively, we can develop our own user interface (UI) to monitor UCSM or request server. I will demonstrate a UI I developed before using .Net in the next blog.