Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell script to savestate of VirtualBox VM works but with an error each time

I have written a PowerShell script to start/stop a VM as follows:

$vma is the name of the VM.

$vmstate = (vboxmanage showvminfo $vma --machinereadable | % { if ($_ -like 'VMState="*"') { $_ } })

Write-Host $vmstate

if ($vmstate -like '*run*') {
    Write-Host "Wait while "$vma" is powered off (saving state)"
    vboxmanage controlvm $vma savestate
}
else {
    Write-Host "Wait while "$vma" is started"
    vboxmanage startvm $vma
}

Starting the VM works fine each time. Saving the state works too, but shows this error in the PS ISE output window:

Wait while  W2008_21_06_A is powered off (saving state)
vboxmanage : 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
At C:\Users\craig\My Documents\vbox.ps1:18 char:5
+     vboxmanage controlvm $vma savestate
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (0%...10%...20%....0%...90%...100%:String) [],      RemoteException
    + FullyQualifiedErrorId : NativeCommandError

I get the impression that it has saved the state before VBoxManage has worked out result. Any ideas?

My system specs:

  • VBox v5.1.24
  • PowerShell v4.0
  • Windows 8.1
like image 936
Craig Avatar asked Dec 22 '25 10:12

Craig


2 Answers

Your "problem" is, you are using PowerShell in ISE. Try using PowerShell directly. The difference? Two strange things first:

  1. When using the savestate parameter of vboxmanage, the output on success (0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%) is written to stderr (instead of stdout, what would be more appropriate imo).
  2. ISE throws an exception, if an external program writes to stderr. PowerShell does not do that.

In conclusion, ISE throws an error on an actual success message, because it has been written to stderr. You can prevent this behaviour by using the Start-Process cmdlet to run external programs. You could replace your calls to VBoxManage.exe by

Start-Process "C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" -ArgumentList "controlvm `"$vma`" savestate"

and

Start-Process "C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" -ArgumentList "startvm `"$vma`""

like image 63
stackprotector Avatar answered Dec 24 '25 05:12

stackprotector


I edited the script with full paths to vboxmanage and ran from command line with powershell -f and the error does not occur.

So I guess it is something to do with ISE.

New code:

$vmstate = (& "C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" showvminfo $vma --machinereadable | % { if ($_ -like 'VMState="*"') { $_ } })

Write-Host $vma $vmstate

if ($vmstate -like '*run*')
{
    # Power off VM before restoring snapshot
    Write-Host "Wait while "$vma" is powered off (saving state)"
    & "C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" controlvm $vma savestate
}
else
{
    # Start VM
    Write-Host "Wait while "$vma" is started"
    & "C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" startvm $vma
}

On command line:

powershell -f "C:\Users\craig\My Documents\vbox.ps1"

Don't know if anyone wants to share why ISE would cause the error.

like image 23
Craig Avatar answered Dec 24 '25 04:12

Craig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!