VMware Cloud Community
emmoon1981
Enthusiast
Enthusiast

VIRTUAL MACHINE STARTUP AND SHUTDOWN for list of hosts

Hello!! 

I'm hoping someone can help me with a PowerCLI cmdlet that could put all of vm's for all hosts in Automatic startup in addition set allow virtual machines to start and stop automatically and set delay shutdown and shutdown action  for each vms in all host .@

Best regards

Ehsan

0 Kudos
46 Replies
LucD
Leadership
Leadership

Try something like this

$startDelaySeconds = 60

$stopDelaySeconds = 60

Get-VMHost | Get-VMHostStartPolicy | Set-VMHostStartPolicy -Enabled -StartDelay $startDelaySeconds -StopDelay $stopDelaySeconds -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
emmoon1981
Enthusiast
Enthusiast

Acctualy  I  try your Cmdlet below

"Get-VMHost | Get-VMHostStartPolicy | Set-VMHostStartPolicy -Enabled:$true -StartDelay $startDelaySeconds -StopDelay $stopDelaySeconds -stopaction guest -Confirm:$false:

and everything is OK just it doesn't put all vm's in Automatic Startup Order

0 Kudos
LucD
Leadership
Leadership

That could mean you already had individual settings for the VMs.

The lines above change the setting on the VMHost level, and will only impact the VMs that are set to default.

Could you perhaps add a screenshot of which exact settings you want to change ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
emmoon1981
Enthusiast
Enthusiast

Here is the screenshot of Before , after and my expected settings. The last one was set manually by using vsphere client. and I want to do it by PowerCLI cmdlet.

0 Kudos
LucD
Leadership
Leadership

Ok, try like this

$esxName = 'MyEsx'

$esx = Get-VMHost -Name $esxName

$autoMgr = Get-view $esx.ExtensionData.ConfigManager.AutoStartManager

$spec = New-Object VMware.Vim.HostAutoStartManagerConfig

$spec.Defaults = New-Object VMware.Vim.AutoStartDefaults

$spec.Defaults.enabled = $true

$spec.Defaults.startDelay = 60

$spec.Defaults.stopAction = 'PowerOff'

$spec.Defaults.stopDelay = 60

$spec.Defaults.waitForHeartbeat = 'yes'

$order = 1

$esx.ExtensionData.Vm | %{

    $vmSpec = New-Object VMware.Vim.AutoStartPowerInfo

    $vmSpec.Key = $_

    $vmSpec.startAction = 'powerON'

    $vmSpec.startDelay = -1

    $vmSpec.startOrder = $order

    $vmSpec.stopAction = 'powerOff'

    $vmSpec.stopDelay = 60

    $vmSpec.waitForHeartbeat = 'yes'

    $spec.powerInfo += $vmSpec

    $order++

}

$autoMgr.ReconfigureAutostart($spec)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
emmoon1981
Enthusiast
Enthusiast

Actually I tried that  and it works for "poweroff" stop action and put all VMs into automatic startup order .I am not sure is it possible to put VMs in specific order for example I have 2 hosts and 6 virtual machines for each on like this

Host 01                                        Host02

APP1.test01.org                       APP1.test02.org

APP2.test01.org                       APP2.test02.org

DB1.test01.org                         DB1.test02.org

DB2.test01.org                         DB2.test02.org

AD.test01.org                           AD.test02.org

WSUS.test01.org                     WSUS.test02.org

I mean at the Automatically startup order sometings like this : first is APP1 then APP2 after  that DB1 and ...

also I want to change Shutdown action to "Guest shutdown" so I put host01 and host02 in the text file that name is hosts.txt

and  change your cmdlet  to something like this

$vihosts= Get-Content "c:\hosts.txt"
foreach ($singleViserver in $vihosts){
Connect-VIServer $singleViserver -User root -Password mypass
$esx = Get-VMHost -Name $vihosts

$autoMgr = Get-view $esx.ExtensionData.ConfigManager.AutoStartManager

$spec = New-Object VMware.Vim.HostAutoStartManagerConfig

$spec.Defaults = New-Object VMware.Vim.AutoStartDefaults

$spec.Defaults.enabled = $true

$spec.Defaults.startDelay = 30

$spec.Defaults.stopAction = 'GuestShutdown'

$spec.Defaults.stopDelay = 30

$spec.Defaults.waitForHeartbeat = 'yes'

$order = 1

$esx.ExtensionData.Vm | %{

    $vmSpec = New-Object VMware.Vim.AutoStartPowerInfo

    $vmSpec.Key = $_

    $vmSpec.startAction = 'powerON'

    $vmSpec.startDelay = -1

    $vmSpec.startOrder = $order

    $vmSpec.stopAction = 'GuestShutdown'

    $vmSpec.stopDelay = 30

    $vmSpec.waitForHeartbeat = 'yes'

    $spec.powerInfo += $vmSpec

    $order++

}

$autoMgr.ReconfigureAutostart($spec)


Disconnect-VIServer -Confirm:$False

}

now which change I should take to this cmdlet to have my order list?

Thanks.

0 Kudos
LucD
Leadership
Leadership

Try like this

It assumes that there is a text file per host which contains the order of the VMs, and the name of that file should be something like host01.txt

$vihosts= Get-Content "c:\hosts.txt"

foreach ($singleViserver in $vihosts){

    Connect-VIServer $singleViserver -User root -Password mypass

    # Read order from file (<hostname>.txt)

    $order = 1

    $vmTab = @{}

    Get-Content "C:\$($singleViserver).txt" | %{

        $vm = Get-VM -Name $_

        $vmTab.Add($vm.ExtensionData.MoRef,$order)

        $order++

    }

    $esx = Get-VMHost -Name $singleViserver

    $autoMgr = Get-view $esx.ExtensionData.ConfigManager.AutoStartManager

    

    $spec = New-Object VMware.Vim.HostAutoStartManagerConfig

    $spec.Defaults = New-Object VMware.Vim.AutoStartDefaults

    $spec.Defaults.enabled = $true

    $spec.Defaults.startDelay = 30

    $spec.Defaults.stopAction = 'GuestShutdown'

    $spec.Defaults.stopDelay = 30

    $spec.Defaults.waitForHeartbeat = 'yes'

    

    $esx.ExtensionData.Vm | %{

        $vmSpec = New-Object VMware.Vim.AutoStartPowerInfo

        $vmSpec.Key = $_

        $vmSpec.startAction = 'powerON'

        $vmSpec.startDelay = -1

        $vmSpec.startOrder = $vmTab[$_]

        $vmSpec.stopAction = 'GuestShutdown'

        $vmSpec.stopDelay = 30

        $vmSpec.waitForHeartbeat = 'yes'

    

        $spec.powerInfo += $vmSpec

        $order++

    }

    

    $autoMgr.ReconfigureAutostart($spec)

   

    Disconnect-VIServer -Confirm:$False

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
emmoon1981
Enthusiast
Enthusiast

I try that  unfortunately I see this error and nothing changed.Smiley Sad

also I create a text file per host which contains the order of the VMs.

0 Kudos
LucD
Leadership
Leadership

Do you have VMs on that ESX node, that are not in the .txt file ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership

If yes, try like this

$vihosts= Get-Content "c:\hosts.txt"

foreach ($singleViserver in $vihosts){

#    Connect-VIServer $singleViserver -User root -Password mypass

    # Read order from file (<hostname>.txt)

    $order = 1

    $vmTab = @{}

    Get-Content "C:\$($singleViserver).txt" | %{

        $vm = Get-VM -Name $_

        $vmTab.Add($vm.ExtensionData.MoRef,$order)

        $order++

    }

    $esx = Get-VMHost -Name $singleViserver

    $autoMgr = Get-view $esx.ExtensionData.ConfigManager.AutoStartManager

   

    $spec = New-Object VMware.Vim.HostAutoStartManagerConfig

    $spec.Defaults = New-Object VMware.Vim.AutoStartDefaults

    $spec.Defaults.enabled = $true

    $spec.Defaults.startDelay = 30

    $spec.Defaults.stopAction = 'GuestShutdown'

    $spec.Defaults.stopDelay = 30

    $spec.Defaults.waitForHeartbeat = 'yes'

   

    $esx.ExtensionData.Vm | %{

        if($vmTab.ContainsKey($_)){

            $vmSpec = New-Object VMware.Vim.AutoStartPowerInfo

            $vmSpec.Key = $_

            $vmSpec.startAction = 'powerON'

            $vmSpec.startDelay = -1

            $vmSpec.startOrder = $vmTab[$_]

            $vmSpec.stopAction = 'GuestShutdown'

            $vmSpec.stopDelay = 30

            $vmSpec.waitForHeartbeat = 'yes'

       

            $spec.powerInfo += $vmSpec

        }

    }

   

    $autoMgr.ReconfigureAutostart($spec)

  

#    Disconnect-VIServer -Confirm:$False


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
emmoon1981
Enthusiast
Enthusiast

No I put all VMs in each host.txt   files .

0 Kudos
LucD
Leadership
Leadership

Did you try that last version of the script ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
emmoon1981
Enthusiast
Enthusiast

I try your last script and again see the same error . in addition I try with another host.

0 Kudos
LucD
Leadership
Leadership

Seems to work for me.
I would need to see the content of the $spec variable, and the errors in the vpxd log to analyse what goes wrong in your case.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
emmoon1981
Enthusiast
Enthusiast

Sorry I don't know what is "the content of the $spec variable" and where is located "vpxd log" (I search for vpxd log in "/var/log" and didn't find anythings match with this )

0 Kudos
LucD
Leadership
Leadership

The $spec variable is the parameter block that is passed to the method.

After you run the script, you can do a

$spec | Format-Custom -Depth 3

That will dump the content of the variable to the console.

Attach the output as a file to the thread.

The vpxd is the vCenter log.

But are you connecting to a vCenter or directly to the ESXi node ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
emmoon1981
Enthusiast
Enthusiast

after the script I ran "$spec | Format-Custom -Depth 3"

and see the .txt(attached)

Each Host Connected to vcenter server  but I connected to each one separately .

0 Kudos
emmoon1981
Enthusiast
Enthusiast

PowerCLI C:\> $spec | Format-Custom -Depth 3

class HostAutoStartManagerConfig
{
  Defaults =
    class AutoStartDefaults
    {
      Enabled = True
      StartDelay = 30
      StopDelay = 30
      WaitForHeartbeat = True
      StopAction = GuestShutdown
    }
  PowerInfo =
    [
      class AutoStartPowerInfo
      {
        Key =
          class ManagedObjectReference
          {
            Type = VirtualMachine
            Value = 10
          }
        StartOrder = 2
        StartDelay = -1
        WaitForHeartbeat = yes
        StartAction = powerON
        StopDelay = 30
        StopAction = GuestShutdown
        LinkedView =
      }
      class AutoStartPowerInfo
      {
        Key =
          class ManagedObjectReference
          {
            Type = VirtualMachine
            Value = 11
          }
        StartOrder = 7
        StartDelay = -1
        WaitForHeartbeat = yes
        StartAction = powerON
        StopDelay = 30
        StopAction = GuestShutdown
        LinkedView =
      }
      class AutoStartPowerInfo
      {
        Key =
          class ManagedObjectReference
          {
            Type = VirtualMachine
            Value = 12
          }
        StartOrder = 5
        StartDelay = -1
        WaitForHeartbeat = yes
        StartAction = powerON
        StopDelay = 30
        StopAction = GuestShutdown
        LinkedView =
      }
      class AutoStartPowerInfo
      {
        Key =
          class ManagedObjectReference
          {
            Type = VirtualMachine
            Value = 13
          }
        StartOrder = 1
        StartDelay = -1
        WaitForHeartbeat = yes
        StartAction = powerON
        StopDelay = 30
        StopAction = GuestShutdown
        LinkedView =
      }
      ...
    ]

}

PowerCLI C:\>

0 Kudos
LucD
Leadership
Leadership

IN the StartOrder property I see the values 1,2,5 and 7.

I would expect sequential numbers in there (1,2,3,4)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos