Platinum Sponsor

PowerCLI: Lesson #6

This lesson I thought I’d talk about some realworld stuff.  In my lab I had a need to use VMware DPM to reduce the heat my home servers where creating by shutting down 1 server while not needed. What I wanted though was to control at what point’s through the day DPM was enabled or disabled, and when disabled both my lab systems should be powered up.

Asked for help on this one from Luc Dekens – as control of DPM wasn’t so obvious and Luc pointed me to this:

I looked at it and thought uh! But then a few memories started to trigger in my brain. Having previously programmed the SDK in C# I recognized that this was calling the SDK direct some way.  So let’s break it down line by line an try and find out what’s going on:

$cluster = Get-Cluster <clustername> | Get-View

DPM is configured in the settings of a cluster. The line above declares a variable $cluster and then we pass in the results of the Get-Cluster cmdlet which in turn was piped through the Get-View cmdlet.

The Get-Cluster part will return the status of the specified cluster but it’s not enough and this is where piping it into Get-View helps. This will return the fine detail about the cluster including its’ MoRef.

In the script where we see VMware.Vim.~~~ this refers to the use of the VI SDK API.

The next line sets up a custom specification of type: ClusterConfigSpecEx . A custom specification is an object which directs the SDK calls down the right road.

$spec = New-Object VMware.Vim.ClusterConfigSpecEx

So now $spec is an object in its own right with properties.

The next thing we have to do is declare to .dpmconfig property of the $spec object as a ClusterDpmConfigInfo property.

$spec.dpmConfig = New-Object VMware.Vim.ClusterDpmConfigInfo

This will allow us to influence the properties of Clusters DPM properties.

Now we even have properties of $spec.dpmconfig object and in our case it’s the enabled property which is of interest to us:

$spec.dpmConfig.enabled = $true

Changing this from $true to $false is the same as enabling and disabling DPM.

The last line performs the changes.

$taskMoRef = $cluster.ReconfigureComputeResource_Task($spec, $true)

The $taskMoref is a pointer to the Task object which will perform the task based on the criteria. The criteria being  the cluster object . The task is delivered through method execution, in our case ReconfigureComputeResource. This method requires the parameters as specified in the $spec object.

When there isn’t a relevant PowerCLI cmdlet you can fall back on calling the SDK APIs. In this example we are using the ReconfigureComputeResource method that is available on the ComputeResource managed object. One of the parameters for this method are passed through the ClusterConfigSpecEx object.

Now this is a bit much to ingest for a beginner but this is something we are going to have to get used to over time.

My best advice to you is study the following resources:

Now back to the job in hand.

We still need to do a bit more to fully automate this operation.

I needed to execute the scripts I create by scheduling them. After saving my scripts (DPMOFF.ps1 & DPMON.ps1) I used the windows scheduler (boooo, I know) to execute them at given times. To execute them, the scripts have to be passed into the powershell.exe command like so:

powershell.exe c:\dpmon.ps1

But executing the script as shown above isn’t enough. It would fail in its current state. First of all we have to tell powershell to use the VMware API snapin with the following line:

Add-PSSnapin VMware.VimAutomation.Core

Next thing we need to do is connect to the relevant vCenter server so you don’t need to be there to manually connect:

Connect-VIServer -server vctest -user administrator -password passwd

Now on the DPMOFF (not DPMON.ps1) script I needed to make sure the hosts where out of stand-by mode:

Start-VMHost esx1.virtualplanet.local -RunAsync -Confirm:$false

Start-VMHost esx2.virtualplanet.local -RunAsync -Confirm:$false

This starts each host, doesn’t prompt you for a “are you sure” question and doesn’t make the script wait until the hosts are powered.

Job done.

So the script looks like this:

That’s it for this lesson.

Previous Next

Leave a Reply

Gold Sponsors
Silver Sponsors