none
Shutting down a Hyper-V VM with a Powershell script

    Pregunta

  • I have been trying to use a script I found to shutdown and backup my hyperv vms.  I get an error everytime I try and run it.

    The script below is one easily found online, I have tried many variations but always seem to get the same error.  Any help is appreciated!

    You cannot call a method on a null-valued expression.
    At C:\scripts\VmBackup\test2.ps1:30 char:35
    + $result = $vmshut.InitiateShutdown <<<< ("$true","no comment")
        + CategoryInfo          : InvalidOperation: (InitiateShutdown:String) [],
       RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    This is line 30 of the script-$result = $vmshut.InitiateShutdown("$true","no comment")

    $waitstart = 200
    $waitshutdown = 120

    if ($args[1] -match "0") {
    $inputfile=get-content $args[0]
    foreach ($guest in $inputfile) {
    write-host "Starting $guest"
    $vm = gwmi -namespace root\virtualization -query "select * from msvm_computersystem where elementname='$guest'"
    $result = $vm.requeststatechange(2)
    if ($result.returnvalue -match "0") {
    start-sleep -s $waitstart
    write-host ""
    write-host "$guest is started" -foregroundcolor green
    write-host ""
    }
    else {
    write-host ""
    write-host "unable to start $guest" -foregroundcolor red
    write-host ""
    }}}

    if ($args[1] -match "1") {
    $inputfile=get-content $args[0]
    foreach ($guest in $inputfile) {
    write-host "shutting down $guest"
    $vm = gwmi -namespace root\virtualization -query "select * from msvm_computersystem where elementname='$guest'"
    $vmname = $vm.name
    $vmshut = gwmi -namespace root\virtualization -query "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='$vmname'"
    $result = $vmshut.InitiateShutdown("$true","no comment")
    if ($result.returnvalue -match "0") {
    start-sleep -s $waitshutdown
    write-host ""
    write-host "no error while shutting down $guest"
    write-host "shutdown of $guest completed" -foregroundcolor green
    write-host ""}

    else {
    write-host ""
    write-host "unable to shutdown $guest" -foregroundcolor red
    write-host ""
    }}}

    else {
    write-host "USAGE: to shutdown VMs," -nonewline; write-host ".\managehyperV.ps1 c:\hosts.txt 1" -foregroundcolor yellow
    write-host "USAGE: to start VMs," -nonewline; write-host ".\managehyperV.ps1 c:\hosts.txt 0" -foregroundcolor yellow
    }

    miércoles, 09 de marzo de 2011 3:44

Respuestas

  • Your variable $vmshut is being returned "empty" - that is the root of the error.

    Therefore the method "InitiateShutdown" is not available.  That is a method that is part of the VM object itself - you have no object you have no function.

    Just glancing at what you have here, this looks like it needs to run directly on the Hyper-V server itself.  As the WMI calls seem to assume "localhost"

    I have never used this script - but it is looking for arguments to be passed to the script when you execute it.  Har are you invoking the full script?

    Also, did you go back to the source of the script and sk for any assistance?

     


    Brian Ehlert (hopefully you have found this useful) http://ITProctology.blogspot.com
    miércoles, 09 de marzo de 2011 4:21
    Moderador

Todas las respuestas

  • Your variable $vmshut is being returned "empty" - that is the root of the error.

    Therefore the method "InitiateShutdown" is not available.  That is a method that is part of the VM object itself - you have no object you have no function.

    Just glancing at what you have here, this looks like it needs to run directly on the Hyper-V server itself.  As the WMI calls seem to assume "localhost"

    I have never used this script - but it is looking for arguments to be passed to the script when you execute it.  Har are you invoking the full script?

    Also, did you go back to the source of the script and sk for any assistance?

     


    Brian Ehlert (hopefully you have found this useful) http://ITProctology.blogspot.com
    miércoles, 09 de marzo de 2011 4:21
    Moderador
  • I was having this exact problem while running it on the host.  I was using a batch file via task scheduler.  When I would manually run the batch file it worked, but the scheduled task got this error.  Once I changed the property on the scheduled task to run with elevated priviledges it worked on the scheduled task.
    viernes, 22 de abril de 2011 14:39
  • "Just glancing at what you have here, this looks like it needs to run directly on the Hyper-V server itself.  As the WMI calls seem to assume "localhost"

    That would be my best guess.  Have you tried the PShyper-V cmdlets?  http://pshyperv.codeplex.com/  Try Invoke-VMShutdown with the -Server parameter.

     

    J

     

    viernes, 22 de abril de 2011 17:08