PowerShell to Automate Cisco UCSM (4): PS with .Net to develop UI and provision service profile

PowerShell to Automate Cisco UCSM (4): PS with .Net to develop UI and provision service profile

Introduction PowerShell to Automate UCS (1): Lab Setup introduced the UCSM and PowerTool lab setup. PowerShell to Automate UCS (2): PowerGUI Editor in

PowerShell to Automate Cisco UCSM (2): PowerGUI Editor
PowerShell to Automate Cisco UCSM (1): Lab Setup
PowerShell to Automate Cisco UCSM (3): Convert UCSM GUI to Script

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.

UCSM_inventory.png

If you experience the same, set the UCSPE to factory default, which forces clearing its database and re-inserting devices.

UCSM_factoryreset.png

PS and .NET Script

PowerGUI was used to develop and test the script. Please refer to PowerShell to Automate UCS (2): PowerGUI Editor.

##########################################################################
#UCSM Automation-provision UCS service profile,dotNet generate server list #
#By MengMeng 17/07/2016                                                  #
##########################################################################
clear

#Import Cisco UCS PowerTool Module
Import-Module CiscoUcsPS

#Input variables here
$ucsvip= "10.1.200.230"
$sptname = "sptemplate"
$spname = "spprofile2"

#Auto logon UCSM
$user = "ucspe"
$password = "ucspe" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object system.Management.Automation.PSCredential ($user,$password)
Connect-Ucs $ucsvip -Credential $cred

#Create service profile from service profile template
Get-UcsOrg -Level root | Get-UcsServiceProfile -Name $sptname -LimitScope | Add-UcsServiceProfileFromTemplate -NewName @($spname) -DestinationOrg "org-root" 

#Get a list a available hosts. @() to create array
$hostused = @()
#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.
$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")

# If a server name exists in the $hostused array, then mark it as [USED] in the $hostavailable array.
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
}

# Use .NET to create Windows pop-up selection list for available host selection
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

#Draw the popup window
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Select a Server"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

#Define selection action,i.e.capture the selected variable and close the popup window.
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})
#Create OK button
$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)

#Create Cancel button
$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) 

# Draw selection list
$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

# Add each variable from $hostavailable array in the created selection list
$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 

#Capture the selected value to variable $hostchosen
$hostchosen = $x

# Associate service profile to host
Get-UcsOrg -level root | Get-UcsServiceProfile -Name $spname -LimitScope | Add-UcsLsBinding -ModifyPresent -PnDn $hostchosen -RestrictMigration "no" 

#Disconnect from the UCSM
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.

dotnet_ps.png

Let’s access to UCSM GUI and verify whether the server is successfully provisioned.

spprofile_provisioned.png

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

WORDPRESS: 0
DISQUS: 0