HOWTO: Power on your IPMI connected computer using PowerShell



Microsoft PowerShell Home Lab

Published on 25 April 2016 by Christopher Lewis. Words: 416. Reading Time: 2 mins.

As part of my VMware home lab setup I wanted to be able to run a PowerCLI/PowerShell script to Power On/Off my hosts as required. The first objective was being able to power the hosts on using their IPMI interfaces.

Thankfully the Microsoft PowerShell has something that does the job quite nicely called Physical Computer System View (PCSV) Device Cmdlets.

Using Get-PcsvDevice

Using the Get-PcsvDevice command interactively will require you to provide an IP Address/Hostname, a protocol (such as IPMI) and a set of credentials.

Just using Get-PcsvDevice <ip address> will immediately prompt you to provide with the Windows PowerShell credential request dialog.

Even when you get passed this initial dialog, you will then need to supply the Management Protocol to be used.

However, within a PowerShell script, you can easily provide all the relevant information on a command line.

Get-PcsvDevice TargetAddress <ipmi ip> ManagementProtocol IPMI Credential <credentialobject>

Whoa wait a second, How do I pass Credential Object? Well that’s easy… I’ll call my PSCredential object $Credential

$Credential = New-Object TypeName System.Management.Automation.PSCredential ArgumentList = <user id>, <encrypted password>

Whoa wait another goddamn second, how do i created an encrypted password to be able to create the credential object to be able to power on my server via IPMI? Well that’s easy too… I’ll call my password object $EncryptedPassword.

$EncryptedPassword = ConvertTo-SecureString String "<password>" AsPlainText Force

Putting that all together within a PowerShell script, it may look something like this:

$EncryptedPassword = ConvertTo-SecureString -String "$Password" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $EncryptedPassword
Get-PcsvDevice -TargetAddress $IPAddress -ManagementProtocol IPMI -Credential $Credential

Now like most PowerShell cmdlets you use a combination of Get-PcsvDevice and pipe the output into Start-PcsvDevice, Restart-PcsvDevice or Stop-PcsvDevice. If you then also add some text out to the console to show what your actually doing… it may end up looking like this.

Function StartHost ([string]$ServerName, [string]$IPAddress, [string]$Password, [string]$User)
{
$EncryptedPassword = ConvertTo-SecureString -String "$Password" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $EncryptedPassword
Write-Host "Powering on $ServerName using IPMI ($IPAddress)" -ForegroundColor Green
Get-PcsvDevice -TargetAddress $IPAddress -ManagementProtocol IPMI -Credential $Credential | Start-PcsvDevice
}

To call that function within the PowerShell script you would just use:

StartHost "<host name>" "<IPMI address>" "<user>" "<password>"

Note: the variable is only passed to the function to enable descriptive output.

I’m sure the PowerShell Wizards may be able to make the above code more efficient, but for my homelab environment at my current PowerShell skill level, it does the job (as part of a wider homelab startup script).

Published on 25 April 2016 by Christopher Lewis. Words: 416. Reading Time: 2 mins.