Monday, January 30, 2012

Getting convert VHD jobs per VM from SCVMM 2012

I have been running some tests recently and one of the things that was brought into question is if a disk conversion job is taking longer to run as the number of VMs increases in the environment.

Now, you might think this is relatively easy.  Just issue a query with Get-SCJob and filter.

Well, I did begin that way, thinking I would get just what I wanted.

Get-SCJob | where { $_.Name -eq "convert virtual hard disk" } | where { $_.Status -eq "Completed" }

But I never got the right number of VHD conversion jobs returned back.  This obviously needed more complex scripting.

So, I tried using Get-SCVirtualDiskDrive   That cmdlet requires a VM object to be tossed in, I can’t just get a list of all the VHD like I could through a CIM call to a hypervisor.

Now I have to go to the VM level.  I have to query for the VMs, then find each virtual disk, then get the most recent job object on that virtual disk.

$scVMs = Get-SCVirtualMachine | where {$_.Name -match "Xd1K"}

In a long hand way that looks like this:

Foreach ($scVM in $scVMs){
    $vmVhd = Get-SCVirtualDiskDrive -VM $scVM | where {$_.MostRecentTask -match "convert"} 
    $vhdJob = $vmVhd.MostRecentTask
    $vhdConvertJobs += [Microsoft.SystemCenter.VirtualMachineManager.Task]$vhdJob

Take notice of that object cast; [Microsoft.SystemCenter.VirtualMachineManager.Task].  If you don’t do this you get an error that the Task cannot be converted to int32.  Don’t ask me why PowerShell is trying to convert the Task object into an integer, but that is what the default is trying to do.

In a nice tight shorthand way it looks like this:

Foreach ($scVM in $scVMs){
    $vhdConvertJobs += [Microsoft.SystemCenter.VirtualMachineManager.Task]( Get-SCVirtualDiskDrive -VM $scVM | where {$_.MostRecentTask -match "convert"} ).MostRecentTask

Next time, calculating the average time to execute the jobs, and making it useful to quickly check.

No comments: