Skip to content

Instantly share code, notes, and snippets.

@JayCuthrell
Created September 21, 2010 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayCuthrell/589096 to your computer and use it in GitHub Desktop.
Save JayCuthrell/589096 to your computer and use it in GitHub Desktop.
#####################################################################
# UCS_VMware_IMPI.ps1 v0.01 By: Justin Guidroz #
# #
# This script will connect to UCS Manager, collect IPMI information #
# for the provisioned blades, and update the appropriate IPMI #
# information on the ESX hosts. The script can also be run to #
# update the IPMI information if a service profile is moved to a #
# different blade. #
# #
#####################################################################
Add-PSSnapin VMware.VimAutomation.Core
### Needed variables
$ucsUrl = ""
$ucsUser = ""
$ucsPassword = ""
$vCenterUrl = ""
$vCenterUser = ""
$vCenterPassword = ""
$ipmiUser = ""
$ipmiPassword = ""
### Function ucs_post
### Required variables: $url = UCS Url $data = XML to send to UCS
### Returns: XML response from UCS
function ucs_post($url,$data) {
$request = [System.Net.HttpWebRequest] [System.Net.HttpWebRequest]::Create("http://" + $url +"/nuova")
$request.Method = "POST"
$request.ContentType = "text/xml"
$sendData = new-object System.IO.StreamWriter($request.GetRequestStream())
$sendData.Write($data)
$sendData.Close()
$response = $request.GetResponse()
$sr = new-object System.IO.StreamReader($response.GetResponseStream())
$xml = [xml] $sr.ReadToEnd()
return $xml
}
### Function ucs_login
### Required variables: $inName = UCS username $inPassword = UCS password $url = UCS url
### Returns: Cookie after login
### Todo: Error Checking
function ucs_login($inName, $inPassword, $url) {
$aaaLogin = "<aaaLogin inName='" + $inName + "' inPassword='" + $inPassword + "' />"
$xml = ucs_post $url $aaaLogin
$outCookie = $xml.aaaLogin.outCookie
return $outCookie
}
### Function ucs_logout
### Required variables: $url = UCS url $inCookie = Cookie for session to logout
### Returns: Status of logout
### Todo: Error Checking
function ucs_logout($url, $inCookie) {
$aaaLogout = "<aaaLogout inCookie='" + $inCookie + "' />"
$xml = ucs_post $url $aaaLogout
$outStatus = $xml.aaaLogout.outStatus
return $outStatus
}
### Function get_esx_hosts
### Required variables: $vCenter = $vCenter server object
### Returns: ESX hosts in vCenter
### ToDo: Error checking. More logic
function get_esx_hosts($vCenter) {
$esxhosts = @()
$VMHosts = Get-VMhost -server $vCenter
foreach ($h in $VMHosts) {
$esxhost = "" | Select-Object Name, Uuid, IpmiIp, IpmiMac
$esxhost.Name = $h.name
$v = Get-VMHost -Name $h | Get-View
$esxhost.Uuid = $v.Summary.Hardware.Uuid
$esxhosts += $esxhost
}
return $esxhosts
}
### Function get_blade_dn
### Required variables: $uuid = ESX UUID $url = UCS url $cookie = UCS cookie for session
### Returns: DN of physical blade
### Todo: Error Checking
function get_blade_dn($uuid, $url, $cookie) {
$computeBlade = "<configResolveClass cookie='" + $cookie + "' inHierarchical='false' classId='computeBlade'><inFilter><eq class='computeBlade' property='uuid' value='" + $uuid + "' /></inFilter> </configResolveClass>"
$bladeXml = ucs_post $url $computeBlade
return $bladeXml.configResolveClass.outConfigs.computeBlade.dn
}
### Function get_blade_ipmi
### Required variables: $dn = DN of physical blade $url = UCS url $cookie = UCS cookie for session
### Returns: Management Interface XML response from UCS
### Todo: Error Checking
function get_blade_ipmi($dn, $url, $cookie) {
$mgmtIf = "<configResolveClass cookie='" + $cookie + "' inHierarchical='false' classId='mgmtIf'><inFilter><eq class='mgmtIf' property='dn' value='" + $dn + "/mgmt/if-1' /></inFilter> </configResolveClass>"
$mgmtIfXml = ucs_post $url $mgmtIf
return $mgmtIfXml
}
### Function get_host_ipmi
### Required variables: $esxhost = ESX Host object $url = UCS url $cookie = UCS cookie for session
### Returns: Updated ESX host object
### Todo: Error checking
function get_host_ipmi($esxhost, $url, $cookie) {
$bladeDn = get_blade_dn $esxhost.Uuid $url $cookie
$mgmtIfXml = get_blade_ipmi $bladeDn $url $cookie
$esxhost.IpmiIp = $mgmtIfXml.configResolveClass.outConfigs.mgmtIf.extIp
$esxhost.IpmiMac = $mgmtIfXml.configResolveClass.outConfigs.mgmtIf.mac
return $esxhost
}
### Function set_host_ipmi
### Required variables: $esxhost = ESX host object $vCenter = vCenter Server Object
### Returns: nothing (should be changed)
### Todo: Error checking
function set_host_ipmi($esxhost, $vCenter) {
$v = Get-VMHost -server $vCenter -Name $esxhost.Name | % {Get-View $_.Id}
$ipmi = New-Object Vmware.Vim.HostIpmiInfo
$ipmi.BmcIpAddress = $esxhost.IpmiIp
$ipmi.BmcMacAddress = $esxhost.IpmiMac
$ipmi.Login = $ipmiUser
$ipmi.Password = $ipmiPassword
$v.UpdateIpmi($ipmi)
}
### Where the fun begins
### Lets log in to vCenter and UCS
$vCenter = Connect-VIServer -server $vCenterUrl -user $vCenterUser -password $vCenterPassword
$cookie = ucs_login $ucsUser $ucsPassword $ucsUrl
### Grabbing ESX hosts from vCenter
Write-Host "Getting ESX Hosts from vCenter"
$esxhosts = get_esx_hosts $vCenter
### Get the IPMI settings from UCS and update ESX hosts with information
Write-Host "Getting IPMI Settings from UCS and configuring ESX"
foreach ($h in $esxhosts) {
$h = get_host_ipmi $h $ucsUrl $cookie
set_host_ipmi $h $vCenter
}
### Fun as ended, time to log out.
Write-Host "Logging out of UCS"
$outStatus = ucs_logout $ucsUrl $cookie
Write-Host $outStatus
Write-Host "Logging out of vCenter"
Disconnect-VIServer -server $server -confirm:$false
@JayCuthrell
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment