Monitoring VMM Jobs with JobVariable

With the -JobVariable parameter you can create a temporary variable that contains the status (and other good stuff) of the created job.
This variable will only be available in the current session, it will not be globally available.

The jobvariable will be converted in a variable, you can only set text as -jobvariable.
I used “CreateVMJob”, and this will be converted to $CreateVMJob. See below for a example.

# Filename:        SimpleCreateVM.ps1
# Description:     Creates a new virtual machine


# Connect to the VMM server
$VMMServer = Get-VMMServer "VMMServer01"
 
# Define the variables
$Template = Get-Template | where {$_.Name -eq "Template01"}
$VMHost = Get-VMHost | where {$_.ComputerName -eq "VMHost1"}
$VMName = "VM01"
$path = "C:\ClusterStorage\Volume1"
 

# Use the New-SCVirtualMachine cmdlet  
$createvm = New-SCVirtualMachine -template $template -Name $VMname -VMhost $VMHost -path $path -RunAsynchronously -jobvariable "CreateVMJob"
 
# Show all content of jobvariable
Write-Host $CreateVMJob | FL *

# Display the job progress.
While ($CreateVMJob.status -eq "Running")
{
   Write-Progress -activity "Creating new virtual machine" -status $CreateVMJob.progress
   Start-Sleep -seconds 3
}

Leave a Reply

Your email address will not be published. Required fields are marked *