Need a way to Cancel an Export-VM Job while it's running - Help

Dec 8, 2009 at 10:45 PM


We had an issue with a NIC card dropping down to <100 mbit over night and our VM exports (backups) did not complete which caused an issue when everyone got in today as one of our VMs was still down and exporting. 

The issue was a faulty cable and is resolved, but we want to extend our powershell scripts so we can cancel a currently exporting vm if this happens again.  Obviously this is possible as I can cancel the job from the Hyper-V manager. 

I can get the GUID "InstanceID" of the job when I run Export-VM, but I can also see it when I look at the the properties of the job when I call:

(Get-VMState $vm).Jobs

where $vm is one of our VMs.

However, even though I have the InstanceID, when I call Stop-Job or Remove-Job I get an error that the job does not exist.  Test-WMIJob returns successfully with the supplied Job object.

Furthermore, if I run a simple asynchronous Export-VM command (outside my script) and then run Get-Job it returns nothing (if run with no parameters it should return all jobs in the session). 

Is all this because the job is a WMI Job?  If so is there another way to cancel the job via powershell?


Any help would be appreciated.



Dec 8, 2009 at 11:13 PM

Powershell's built in -job cmdlets are for managing jobs within a PowerShell session which are a different thing.

Test-wmiJob returns the WMI object representing the task - it should be an MSVM_concreteJob

if you do

$J= Test-WmiJob <id>

MSDN says [] you should be able to call


the 4 is "terminate cleanly", you can use a 5 for "Kill" - the 0 specifies no timeout.

I haven't tried this though [Meaning ... if you try it let me know how it works ]

Dec 9, 2009 at 9:59 PM

Thanks a million, that worked, though the PowerShell function complained about the second parameter but worked with a single parameter to specify the desired state.

Jan 27, 2012 at 1:06 AM

Hi everyone,

I am hoping that you can help another newbie.

I currently have the script that lists the machines I need to start/stop in different files. This is great, however I noticed that it runs all of the exports simultaneously. I have been able to manually check the process of the job using test-wmijob but is there a way of writing that instanceid to a variable every time a export is started so I can have a while loop wrapping my next to export the next machine?

while instanceid.status = running

 I am happy to turn them all off but it would be great to export one at a time.

if ($args[1] -match "2"){ $inputfile=get-content $args[0] $date = get-date -uformat "%d_%m_%y"
 foreach ($guest in $inputfile) {  $vm = gwmi -namespace root\virtualization -query "select * from msvm_computersystem where   elementname='$guest'"  $vmname = $vm.elementname  $exportPath = "\\XXXXXX\HyberbackUp_C\$date"  export-vm -vm $vmname -path $exportpath -copystate -force }}


Any help would be much apprecited.