PowerCLI

 View Only
  • 1.  shutdown vm and wait till it is powered off and make changes

    Posted Jul 03, 2018 04:06 PM

    I am having trouble getting the system to shutdown and wait till it is actually in a state to make changes. I get errors  "the system is not a state... "
    I think the issue is there is a delay from when the system shuts down to when you can actually make a change, such as number of processors.

    Is there a better way than what I am trying below?

    Get-VM -name $VMName* | Shutdown-VMGuest -Confirm:$false

    if ($VMName.PowerState -eq "PoweredOn") {

       Write-Host "Shutting Down" $VMName

       Shutdown-VMGuest -VM $VMName

       #Wait for Shutdown to complete

       do {

          #Wait 5 seconds

          Start-Sleep -s 5

          $status = $VMName.PowerState

       }until($status -eq "PoweredOff")

    }



  • 2.  RE: shutdown vm and wait till it is powered off and make changes

    Posted Jul 03, 2018 11:42 PM

    Is there a better way to do this?

    $vmName = Read-Host "Enter VM name here"

    If ($vm = Get-VM $vmName)

    {If ($vm.PowerState -eq "PoweredOn") { Write-Host "Shutting down $($vmName)"

    Shutdown-VMGuest -VM $vmName

    Do { #Wait 5 seconds Start-Sleep -Seconds 5

    $status = (Get-VM $vmName).PowerState }

    Until (

    $status -eq "PoweredOff") Start-Sleep -Seconds 15 Write-Host "$($vmName) has shutdown. It should be ready for configuration."

    ## ... } Else { Write-Host "VM '$($vmName)' is not powered on!" } } Else { Write-Host "VM '$($vmName)' not found!" }



  • 3.  RE: shutdown vm and wait till it is powered off and make changes
    Best Answer

    Posted Jul 04, 2018 04:45 AM

    Not really, except leaving out the 2nd Start-Sleep.

    Personally I would do it like this, since this makes it for example easier to include the action to take in case the VM is for example 'Suspended'

    $vmName = Read-Host "Enter VM name here"


    Try{

       $vm = Get-VM -Name $vmName -ErrorAction Stop

       switch($vm.PowerState){

       'poweredon' {

      Shutdown-VMGuest -VM $vm -Confirm:$false

       while($vm.PowerState -eq 'PoweredOn'){

      sleep 5

       $vm = Get-VM -Name $vmName

       }

       }

       Default {

       Write-Host "VM '$($vmName)' is not powered on!"

       }

       }

       Write-Host "$($vmName) has shutdown. It should be ready for configuration."

    }

    Catch{

       Write-Host "VM '$($vmName)' not found!"

    }



  • 4.  RE: shutdown vm and wait till it is powered off and make changes

    Posted Jul 04, 2018 05:54 AM

    The second one is because there keeps occasionally being just enough delay when the vm is powered off and when it can actually be configured, that the first one or 2 operations failed with an error about not in the right state, then the rest would complete. With that extra few seconds, I have not had that issue at all.



  • 5.  RE: shutdown vm and wait till it is powered off and make changes

    Posted Jul 04, 2018 06:04 AM

    Good point, yes, sometimes there is a delay.



  • 6.  RE: shutdown vm and wait till it is powered off and make changes

    Posted Sep 28, 2018 06:35 AM

    How about doing this for the wait... 

    do { $vmstatus = (get-vm $vm).PowerState ; Start-Sleep -Seconds 5} while ($vmstatus -eq "PoweredOn")



  • 7.  RE: shutdown vm and wait till it is powered off and make changes

    Posted Sep 28, 2018 06:49 AM

    That will work, but it is basically the same as what is used in the script.

    The issue seems to be that sometime, while the PowerState reports 'poweredoff', there is a brief period during which you get the "...not in a state...".

    Personally I think the best solution would be to capture that state, in a Try-Catch for example, and loop till that changes.
    But it's hard to test, since the "...not in a state..." happens very infrequently in my experience.



  • 8.  RE: shutdown vm and wait till it is powered off and make changes

    Posted Sep 28, 2018 07:01 AM

    Thanks for the quick response, in that case I guess we can reverse the logic. 

    do { $vmstatus = (get-vm $vm).PowerState ; Start-Sleep -Seconds 5} while ($vmstatus -ne "PoweredOff")

    I am new vmware scripting so trying to figure out what the community is doing as best practice. Thanks for the all your contributions to the community. Came across a lot of your scripts in my recent PowerCLI adventures. 



  • 9.  RE: shutdown vm and wait till it is powered off and make changes

    Posted Sep 28, 2018 07:15 AM

    Thanks.
    I don't really think that makes a difference.
    The intermittent problem seems to be that sometimes, even though the PowerState says poweredoff, you can not yet make a change, that requires a poweredoff VM, to the VM.

    But like I said this is intermittent and doesn't happen in every environment.

    If you don't experience that issue, you code is ok (in both cases).