Introduction PowerShell to Automate UCS (1): Lab Setup introduced the UCSM and PowerTool lab setup. PowerShell to Automate UCS (2): PowerGUI Editor in
Introduction
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.
PowerShell to Automate UCS (3): Convert UCSM GUI to Script introduced how to start from 0 and write PowerShell script to manage UCSM by converting UCSM GUI to script.
This blog demonstrates how to integrate PowerShell and .Net to develop a user interface to check whether a server is in use, then select unused server and provision service profile from the established service profile template.
Preparation
Access to UCSM (we are using UCS platform emulator in this lab) as in PowerShell to Automate UCS (1): Lab Setup. Verify there is an established service profile template and vacant servers to use.
The lab I am using had a sudden power outage last week and corrupted the UCSPE; all chassises and servers disappeared. The equipment inventory (https://x.x.x.x/config/) shows all devices are normal.
If you experience the same, set the UCSPE to factory default, which forces clearing its database and re-inserting devices.
PS and .NET Script
PowerGUI was used to develop and test the script. Please refer to PowerShell to Automate UCS (2): PowerGUI Editor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
<span style="color:#339966;">########################################################################## #UCSM Automation-provision UCS service profile,dotNet generate server list # #By MengMeng 17/07/2016 # ##########################################################################</span> clear <span style="color:#339966;">#Import Cisco UCS PowerTool Module</span> Import-Module CiscoUcsPS <span style="color:#339966;">#Input variables here</span> $ucsvip= "10.1.200.230" $sptname = "sptemplate" $spname = "spprofile2" <span style="color:#339966;">#Auto logon UCSM</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:#339966;">#Create service profile from service profile template</span> Get-UcsOrg -Level root | Get-UcsServiceProfile -Name $sptname -LimitScope | Add-UcsServiceProfileFromTemplate -NewName @($spname) -DestinationOrg "org-root" <span style="color:#339966;">#Get a list a available hosts. @() to create array</span> $hostused = @() <span style="color:#339966;">#Query existing service profile, select associated server, then sort by server name. #UCSM allows to query existing service profile and its associated server; but not server association status directly.</span> $hostused = get-ucsserviceprofile | select PnDn | Sort PnDn $hostavailable = @("sys/chassis-1/blade-1", "sys/chassis-1/blade-2", "sys/chassis-1/blade-3","sys/chassis-1/blade-4","sys/chassis-1/blade-5","sys/chassis-1/blade-6") <span style="color:#339966;"># If a server name exists in the $hostused array, then mark it as [USED] in the $hostavailable array.</span> foreach ($strhostused in $hostused) { if ($strhostused -like "*blade-1*") {$hostavailable[0] = "[USED]sys/chassis-1/blade-1"} elseif ($strhostused -like "*blade-2*") {$hostavailable[1] = "[USED]sys/chassis-1/blade-2"} elseif ($strhostused -like "*blade-3*") {$hostavailable[2] = "[USED]sys/chassis-1/blade-3"} elseif ($strhostused -like "*blade-4*") {$hostavailable[3] = "[USED]sys/server-1/blade-4"} elseif ($strhostused -like "*blade-5*") {$hostavailable[4] = "[USED]sys/chassis-1/blade-5"} elseif ($strhostused -like "*blade-6*") {$hostavailable[5] = "[USED]sys/chassis-1/blade-6"} continue } <span style="color:#339966;"># Use .NET to create Windows pop-up selection list for available host selection</span> [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") <span style="color:#339966;">#Draw the popup window</span> $objForm = New-Object System.Windows.Forms.Form $objForm.Text = "Select a Server" $objForm.Size = New-Object System.Drawing.Size(300,200) $objForm.StartPosition = "CenterScreen" <span style="color:#339966;">#Define selection action,i.e.capture the selected variable and close the popup window.</span> $objForm.KeyPreview = $True $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$x=$objListBox.SelectedItem;$objForm.Close()}}) $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}}) <span style="color:#339966;">#Create OK button</span> $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Size(75,120) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = "OK" $OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()}) $objForm.Controls.Add($OKButton) <span style="color:#339966;">#Create Cancel button</span> $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Size(150,120) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Cancel" $CancelButton.Add_Click({$objForm.Close()}) $objForm.Controls.Add($CancelButton) $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location = New-Object System.Drawing.Size(10,20) $objLabel.Size = New-Object System.Drawing.Size(280,20) $objLabel.Text = "Please select a server:" $objForm.Controls.Add($objLabel) <span style="color:#339966;"># Draw selection list</span> $objListBox = New-Object System.Windows.Forms.ListBox $objListBox.Location = New-Object System.Drawing.Size(10,40) $objListBox.Size = New-Object System.Drawing.Size(260,20) $objListBox.Height = 80 <span style="color:#339966;"># Add each variable from $hostavailable array in the created selection list</span> $hostavailable | ForEach-Object {[void] $objListBox.Items.Add($_)} $objForm.Controls.Add($objListBox) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog() $x = $objListBox.SelectedItem <span style="color:#339966;">#Capture the selected value to variable $hostchosen</span> $hostchosen = $x <span style="color:#339966;"># Associate service profile to host</span> Get-UcsOrg -level root | Get-UcsServiceProfile -Name $spname -LimitScope | Add-UcsLsBinding -ModifyPresent -PnDn $hostchosen -RestrictMigration "no" <span style="color:#339966;">#Disconnect from the UCSM</span> Disconnect-Ucs |
Running Result
Run the script in PowerGUI or natively in PowerShell, a window will popup showing existing server status so that a user can select a unused UCS server to provision.
Let’s access to UCSM GUI and verify whether the server is successfully provisioned.
The End
This is the end of my PowerShell/PowerTool to automate Cisco UCSM series. Hope you find it helpful.
BTW, I am listening to Vishen Lakhiani’s ‘The Code of the Extraordinary Mind’ while writing this post in this sunny Sunday afternoon. Vishen asks what’s the real goal in life? My answer is to love my current and future self. Enjoy every day we have now, don’t leave all fun till retirement. Love ourselves and extend the love to others.
COMMENTS