[Code] PowerTool: PowerOnUCSBlades

May 29, 2013 in Programming3 minutes

  1# ----------------------------------------------------------------------
  2# Name:         PowerOnUCSBlades.ps1                                   
  3# Author:       Matthew Oswalt                                         
  4# Created:      3/30/2012                                              
  5# Revision:     v0.2 - BETA                                                 
  6# Rev. Date:    4/30/2013                                              
  7# Description:  A script that powers on blades in a UCS system.        
  8#               Can be configured to boot all blades, or               
  9#               only those associated to service profiles in a         
 10#               given sub-organization.
 11# ----------------------------------------------------------------------
 12
 13# Import the Cisco UCS PowerTool module
 14Import-Module CiscoUcsPs
 15
 16#Enable Multiple System Config Mode
 17Set-UcsPowerToolConfiguration -SupportMultipleDefaultUcs $true
 18
 19
 20#####################################################################################################################
 21#       AUTHENTICATION             #
 22####################################
 23
 24#Stored method of authentication - change the two values shown below
 25$user = "admin"
 26$password = "password" | ConvertTo-SecureString -AsPlainText -Force
 27$cred = New-Object system.Management.Automation.PSCredential($user, $password)
 28Connect-Ucs 192.168.0.10 -Credential $cred
 29
 30#Connect using "old school" method. This method doesn't store passwords in plain text but less automatable, since you
 31#have to log in every time. You will be prompted for credentials just like you were logging into UCSM.
 32#Connect-Ucs 123.1.2.3 -Credential (Get-Credential)
 33
 34#There is a method of authentication that utilizes encrypted password XML files to allow automation in a secure
 35#fashion, to avoid storing passwords in plain text, but keep the nice automated aspect that comes from not having
 36#to enter credentials every time. That method will be included in future versions.
 37
 38#####################################################################################################################
 39
 40
 41#Initialize Orgs Array
 42$UcsOrgs = @()
 43
 44#Initialize Service Profiles Array
 45$UcsServiceProfiles = @()
 46
 47#Initialize Choices Array
 48[System.Management.Automation.Host.ChoiceDescription[]] $options = @()
 49
 50#Initialize $Line as string
 51$Line = ''
 52
 53#Add Orgs to Array
 54echo 'Getting organizational units from system...'
 55echo ' '
 56echo 'SUBORGS:'
 57echo '================='
 58
 59$counter = 1
 60foreach ($thisOrg in Get-UcsOrg | Select Name)
 61{
 62    $UcsOrgs += $thisOrg
 63    $thisOrgString = $thisOrg.Name.ToString()
 64
 65    echo " $counter. $thisOrgString "
 66    $counter++
 67}
 68
 69#Allow user to enter a number to select desired Org.
 70function getInputFromUser($prompt='Please type the number next to the desired Sub-Org and press <Enter>.') {
 71    Write-Host $prompt
 72    do {
 73        Start-Sleep -milliseconds 100
 74    } until ($Host.UI.RawUI.KeyAvailable)
 75    $thisUserInput = $Host.UI.ReadLine()
 76    $Host.UI.RawUI.FlushInputBuffer()
 77    return $thisUserInput
 78}
 79
 80#Execute above input function and determine name of desired Org.
 81$userInput = getInputFromUser
 82$userInput = [int]$userInput
 83$userInput--
 84$selectedOrg = $UcsOrgs[$userInput].Name.ToString()
 85
 86#Present user with a confirmation dialog
 87$title = "Start Service Profiles"
 88$message = "You have selected $selectedOrg - do you want to start all service profiles in this sub-org?"
 89
 90$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
 91"Reboot all service profiles in this sub-org."
 92
 93$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
 94"Do nothing."
 95
 96$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
 97
 98$result = $host.ui.PromptForChoice($title, $message, $options, 0) 
 99
100switch ($result)
101{
102    0 { #They selected "yes", so proceed with power state change.
103        echo "Powering on all Service Profiles in $selectedOrg ..."
104        $targetedOrg = Get-UcsOrg -Name $selectedOrg
105        
106        #For testing the script, comment this line out to prevent any changes from being made.
107        Get-UcsServiceProfile -Org $targetedOrg | Set-UcsServerPower -State admin-up -Force
108    }
109    1 { #They selected "no" so output a confirmation and continue to quit.
110        echo "You selected No. Exiting..."
111    }
112}
113
114#Disconnect Current Session
115echo DONE
116Disconnect-Ucs