PowerCLI VM Peek

Now that I believe I’ve resolved my hardware issues with my VMware server, I’m expecting to use it much more. I’m also continuing my exploration of the PowerCLI tool set which allows me to manage my virtual infrastructure from a Windows PowerShell session. One task that I frequently need is to identify which virtual machines are running, and especially the guest host name. Last week I tweeted a PowerShell one-liner.
[cc lang=”powerShell”]
get-vm | where {$_.powerstate -match “on”} | get-vmguest | select State,VMName,Hostname,OSFullname,IPAddress
[/cc]
This finds all virtual machines that are powered on then pipes that object to the Get-VMGuest cmdlet which returns the information I’m really after. Here’s a sample result.
[cc lang=”Powershell”]
State : Running
VmName : MyCompany Windows 7
HostName : Win7Desk01.MYCOMPANY.LOCAL
OSFullName : Microsoft Windows 7 (64-bit)
IPAddress : {172.16.10.90}
[/cc]
Now, I could have simply saved that one-liner as a function or script block but naturally I had to take things a step further. I wanted a function that would also include some error handling and handle a connection to my VM host if it wasn’t already connected. Thus was born Get-RunningVM.
[cc lang=”Powershell”]
Function Get-RunningVM {

Param([string]$VMhost=”ESX”,
[string]$protocol=”https”,
[string]$port=”443″,
[string]$user=”root”,
[string]$password)

#verify we’re connected to a VM host
Try {
if (get-vmhost -Server $VMhost -State connected -erroraction “Stop”) {
$connected=$True
}
}
Catch {
#Try to connect
Try {
$viserver=Connect-VIserver -Server $VMhost -protocol $protocol -port $port -user $user -password $password -errorAction “Stop”
$connected=$True
}
Catch {
$msg=”Failed to connect to server {0} on {1}:{2} as user {3}” -f $vmhost,$protocol,$port,$user
Write-Warning $msg
Write-Warning $error[0].Exception.Message
}
}

#if we’re finally connected to a host, get running VMs
if ($connected) {
#get the powered on VMs and display VMGuest information
get-vm | where {$_.powerstate -match “on”} | get-vmguest |
select State,VMName,Hostname,OSFullname,IPAddress
}

} #end function
[/cc]
The function requires that the VMware snapin already be loaded. I have a nested set of Try/Catch constructs to check if I’m connected to VM host and if not, to do so. Once connected I execute my one-liner. I’m using basic, default connection information for my host since I’m not in a large enterprise environment. You might need to tweak the parameters. In fact, since I only have a single VM host, I’m not sure what other tweaks might be necessary for a multiple server environment. I’d love to get some real-world feedback on that. In fact, you will have to edit the default parameter values since the default VMHost is my server name. Using default values saves a lot of typing.

Download Get-RunningVM.

Running Veeam Jobs

I’m still fighting hardware issues with my ESX box (among other things) but I wanted to jot down some more notes on my experiences with PowerCLI and the Veeam backup cmdlets. Last week I wrote about how I created multiple backup jobs with a one line PowerShell expression.  After the jobs were created I needed to run them. For performance purposes I only wanted to run one at a time but I obviously wasn’t going to sit up all night staring at a computer screen.

My answer was to use Veeam’s Start-VBRJob cmdlet. The cmdlet takes a job name as a parameter, which you can pipe to it. All I needed was a collection of job names. I created an empty array, $jobs and then added one job that I knew I still needed for a VM on another datastore. The other new jobs were for VMs on datastore2. Using Get-VBRJob I retrieved all my Veeam jobs, piped them to Where-Object to filter out all jobs except those where the TargetDir property was G:\Datastore2, since I had all my datastore2 jobs backing up to the same location. For each matching backup job, I added the job name to the $jobs array.


Now that I had a collection of jobs, all that was left was to pipe it to Start-VBRJob.

PS C:\> $jobs | Start-VBRJob

The jobs run synchronously and you won’t get your PowerShell prompt back until all the jobs have finished which could be several hours. I was going to let it run overnight so it didn’t matter. But next time I would use a PowerShell job and let this run in the background.


VMware Backup with PowerCLI and Veeam

My primary backup drive for my virtual machine backup toasted on me so I had to recreate all of my backup jobs. I’ve been using Veeam’s backup product for VMware and it couldn’t be easier to use. I’ve known that it included a set of PowerShell cmdlets but I had never really looked at them before.  I decided this would be a great opportunity.

One of my backup goals, that I’ll describe today, was to grab all VMs in a particular datastore and backup them up to a local external drive. The current version of Veeam doesn’t allow you to specify a datastore as a container. Although I didn’t use it, you are supposed to be able to specify an ESX host, which will back up all machines. Or you need to specify a comma separated list of virtual machines. In my case, I wanted all VMs on datastore2. But what are they? PowerCLI to the rescue. First off, your VM host must be connected and not in maintenance mode. For my purposes, I like having the virtual machines themselves powered off. Using Get-VM I can find all machines on the required datastore.

PS C:\> get-vm -Datastore datastore2

Name                 PowerState Num CPUs Memory (MB)
—-                 ———- ——– ———–
MyCompany Windows 7  PoweredOff 1        512
R2 Core RODC         PoweredOff 1        1024
MyCompany Vista      PoweredOff 1        768
MyCompany XP         PoweredOff 1        384
MyCompany2003        PoweredOff 1        384
Research Member S… PoweredOff 2        1024

All I need are the names so I pop them into a variable.

PS C:\> get-vm -Datastore datastore2 | foreach {$v+=$_.name}
PS C:\> $v
MyCompany Windows 7
R2 Core RODC
MyCompany Vista
MyCompany XP
MyCompany2003
Research Member Server R2

Armed with this I can now invoke the Add-VBRBackupJob cmdlet from Veeam.

PS C:\> Add-VBRBackupJob -Name "Dstore2_Backup" -Type VDDK –mode "san;nbd" –Folder "g:\datastore2" -objects $v -host "My Computer" –FileName "dstore2"

This command will create a backup job called Dstore2_Backup using the VMware vStorage API to backup all VM objects to G:\Datastore2 on “My Computer”.  As I continue to poke around with these cmdlets, I’ll be sure to share.

PowerCLI Get-VMToolsVersion

I recently was able to upgrade my VMware server so that I can now fully use the PowerCLI tool set. This is fantastic PowerShell goodness that I hope to use and write about much more in the future. Part of my upgrade process includes upgrading the VMToools install on the virtual machines. But what wasn’t so easy is to pull out the tools version currently installed. The information is there when using Get-View, but it takes a little bit of work. So naturally I developed my own solution.

Continue reading

Waynes World of Tips

I’ve blogged in the past about Wayne Martin and his outstanding list of command line tips. These are one line commands, some complex some simple, that you can use to accomplish a wide range of task. The overall number of tips is to 425 and Wayne recently reorganized them into 7 categories to make it easier for people to digest. There’s very little scripting with any of these commands. Most use native or freely available command line tools. But because they are executed from a command line you could incorporate them into a script. I encourage you to check them out.

The single list:

http://waynes-world-it.blogspot.com/2008/09/useful-command-lines.html

The same commands split into categories:

http://waynes-world-it.blogspot.com/2008/09/useful-active-directory-command-line.html

http://waynes-world-it.blogspot.com/2008/09/useful-dns-dhcp-and-wins-command-line.html

http://waynes-world-it.blogspot.com/2008/09/useful-general-command-line-operations.html

http://waynes-world-it.blogspot.com/2008/09/useful-vmware-esx-and-vc-command-line.html

http://waynes-world-it.blogspot.com/2008/09/useful-windows-mscs-cluster-command.html

http://waynes-world-it.blogspot.com/2008/09/useful-windows-printer-command-line.html

http://waynes-world-it.blogspot.com/2008/09/useful-ntfs-and-security-command-line.html