Platinum Sponsors
Archives

PowerShell Tips Corner

Area dedicated to working with Powersehll in a virtual Environment.

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 – http://www.lucd.info 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:

http://www.vmware.com/pdf/GettingStarted201.pdf

http://www.amazon.com/gp/product/0137153635?ie=UTF8&tag=doublecloudor-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0137153635

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

PowerCLI: Lesson #7 < I'm back in business

Ok I have to make my apologies about this section, it’s been sometime that I have continued these PowerShell Lessons and the reason for that was I was waiting to get my hands on a new PowerCLI book.

 

So now I have a copy I think it’s time for me to once again start these lessons up again and hopefully learn a trick or two from this book. However I still plan to focus on core fundamental PowerShell code for now and try to drop in the odd PowerCLI usage.

In this Lesson we’ll look at spaces, wildcards, escape characters and how they can be used in PowerCLI.

In PowerShell a space is seen as a delimiter between variables and parameters so a typical problem you may run into is when you are querying an object like a VM that has a space in the VM name. vSphere allows you to use the space character in the VM name for example WinXP Test 1 is a legal name for a VM. However if you try to manipulate this VM buy inputting the VM name as is PowerShell will not know that WinXP Test 1 is one piece of text and will fail or will only use the first part of the text WinXP.  To get around this just place the text in quotes, so the VM name would be inputted like so:

 

You can also use single quotes too ‘ but what if you need to input the quotes/single quote symbol as a legal character which is part of the VM name like: WinXP’1 well the answer is simple just surround the VM name with the other quote character. So in our case it would look like this:

 

Ok so what about wildcards? If you were/are familiar with DOS command-line or another shell environments like bash for example these shells often provide a way of performing a query on a group of object opposed to a single object that have common characters in the name of the object. So for example we may want to search the list of VMs that begin with the character ‘v’. To do that is very simple we just enter the asterisks symbol * after specifying the character we want to search on. Here’s an example where we list out only VMs that begin with the letter v:

 

And we are not just restricted to searching against 1 character. This next example shows we can query with 2 characters.  

 

We can use a different wildcards to specify that the query should list out only objects that have a letter at a specific position by using the ? wildcard. So in the above example we could have done the below to list out VMs where the second character is ‘l’ like so:

 

We can also use the [] wildcards to:

specify a range of characters Get-VM – name [m-z]Lab* which would list out any VMs that started with any letter from M to Z and ends with Lab

or

specify multiple characters Get-VM –name [uv]Lab* which would list out any VMs that started with letters U or V and ends with Lab

One last thing I want to show you is the use of the back tick symbol `. One thing PowerShell will allow you to do is escape out of the command line so the structure of the command line can be presented over multiple lines. So coders do this to make it easier to see how they form a command line. Instead of trying to do all on one line we can use the ` to bring us down a line. From here we enter more of the command line and if we want to start another new line we just hit enter.

 

Each new line is started with >> prompt but when you have finished just hit enter one more time on a empty line and the command will execute.

I hope you found this useful. And I promise to bring you more of these lessons in the very near future.

Previous

Gold Sponsors
Silver Sponsor