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

Reply
0 Kudos
46 Replies
LucD
Leadership
Leadership

In esx1.shlalajin.org.txt there is a duplicate line, but that will not explain the 1st error you see.

Just to make sure we are looking at the same thing, can you attach the .ps1 file with the script ?


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

Reply
0 Kudos
emmoon1981
Enthusiast
Enthusiast

I again  check  my VMs name files.txt    and  correct  the  duplicate

Again  I try  your last  scrip

And  see this  error

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

PowerCLI C:\>

PowerCLI C:\>

PowerCLI C:\>

PowerCLI C:\> 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" | %{

>>

>>         Write-Host "Looking at VM $($_), will have order# $($order)"

>>

>>         $vm = Get-VM -Name $_ -ErrorAction SilentlyContinue

>>

>>         if($vm){

>>

>>             Write-Host "Found $($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

>>

>> }

>>

Name                           Port  User

----                           ----  ----

esx1.shmalayer.org             443   root

Looking at VM ShobAD2012.ShMalayer.org, will have order# 1

Found ShobAD2012.ShMalayer.org

Looking at VM Shobdb.Shmalayer.org, will have order# 2

Found Shobdb.Shmalayer.org

Looking at VM ShobDB2.ShMalayer.org, will have order# 3

Found ShobDB2.ShMalayer.org

Looking at VM ShobAPP.ShMalayer.org, will have order# 4

Found ShobAPP.ShMalayer.org

Looking at VM ShobFS.ShMalayer.org, will have order# 5

Found ShobFS.ShMalayer.org

Looking at VM ShobTiba.ShMalayer.org, will have order# 6

Found ShobTiba.ShMalayer.org

Looking at VM ShobWSUS.ShMalayer.org, will have order# 7

Found ShobWSUS.ShMalayer.org

Looking at VM Firewall.ShMalayer.org, will have order# 8

Found Firewall.ShMalayer.org

Exception calling "ReconfigureAutostart" with "1" argument(s): "A specified parameter was not correct: vim.host.AutoStartManager.AutoPowerInfo.startOrder"

At line:37 char:5

+     $autoMgr.ReconfigureAutostart($spec)

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

esx1.shlalajin.org             443   root

Looking at VM ShobAD2012.ShLalajin.org, will have order# 1

Found ShobAD2012.ShLalajin.org

Looking at VM ShobDB.ShLalajin.org, will have order# 2

Found ShobDB.ShLalajin.Org

Looking at VM ShobDB2.ShLalajin.org, will have order# 3

Found ShobDB2.ShLalajin.org

Looking at VM ShobAPP.ShLalajin.org, will have order# 4

Found ShobAPP.ShLalajin.org

Looking at VM ShobTiba.ShLalajin.org, will have order# 5

Found ShobTiba.ShLalajin.org

Looking at VM ShobWSUS.ShLalajin.org, will have order# 6

Found ShobWSUS.ShLalajin.org

Looking at VM Firewall.ShLalajin.org, will have order# 7

Found Firewall.ShLalajin.org

Exception calling "ReconfigureAutostart" with "1" argument(s): "A specified parameter was not correct: vim.host.AutoStartManager.AutoPowerInfo.startOrder"

At line:37 char:5

+     $autoMgr.ReconfigureAutostart($spec)

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

esx1.shnahavand.org            443   root

Looking at VM shobAD2012.ShNahavand.org, will have order# 1

Found ShobAD2012.ShNahavand.org

Looking at VM ShobDB.shNahavand.Org, will have order# 2

Found ShobDB.ShNahavand.Org

Looking at VM ShobDB2.ShNahavand.Org, will have order# 3

Found ShobDB2.ShNahavand.Org

Looking at VM Shobapp.ShNahavand.Org, will have order# 4

Found ShobAPP.ShNahavand.Org

Looking at VM Shobtiba.ShNahavand.org, will have order# 5

Found ShobTiba.ShNahavand.org

Looking at VM ShobwSuS.ShNahavand.org, will have order# 6

Found ShobWSUS.ShNahavand.org

Looking at VM firewall.ShNahavand.Org, will have order# 7

Found Firewall.ShNahavand.Org

Exception calling "ReconfigureAutostart" with "1" argument(s): "A specified parameter was not correct: vim.host.AutoStartManager.AutoPowerInfo.startOrder"

At line:37 char:5

+     $autoMgr.ReconfigureAutostart($spec)

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

esx1.edkolhamedan.org          443   root

Looking at VM EDKolAD2012.EDHamedan.org, will have order# 1

Found EDKolAD2012.EDHamedan.org

Looking at VM EDKolDb.EdHamedan.org, will have order# 2

Found EDKolDb.EdHamedan.org

Looking at VM EDKolApp.EDHamedan.org, will have order# 3

Found EDKolApp.EDHamedan.org

Looking at VM EDFS.EdKolHamedan.org, will have order# 4

Found EDFS.EdKolHamedan.org

Looking at VM EDKolDB2.EDHamedan.org, will have order# 5

Found EDKolDB2.EDHamedan.org

Looking at VM EDKolWSUS.EDHamedan.org, will have order# 6

Found EDKolWSUS.EDHamedan.org

Looking at VM Firewall.EdHamedan.org, will have order# 7

Found Firewall.EdHamedan.org

Looking at VM WHDB.EdKolHamedan.Org, will have order# 8

Found WHDB.EdKolHamedan.Org

Exception calling "ReconfigureAutostart" with "1" argument(s): "A specified parameter was not correct: vim.host.AutoStartManager.AutoPowerInfo.startOrder"

At line:37 char:5

+     $autoMgr.ReconfigureAutostart($spec)

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

PowerCLI C:\>

PowerCLI C:\>

Reply
0 Kudos
LucD
Leadership
Leadership

What is this ?

The error I have been investigating isn't there anymore, while you confirmed several times that it was still there.

error.png


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

Reply
0 Kudos
emmoon1981
Enthusiast
Enthusiast

so I got confused and just did what you told me and your script

and try your script

you mean there is no chance to figure out my problem ? what is wrong ?

Reply
0 Kudos
LucD
Leadership
Leadership

I didn't say that, but you had me on the wrong track.

I was obviously looking at the wrong error.

So you confirm your last run only has this "invalid parameter" issue ?

I'll see if I can come up with some more debugging steps (for me that script seems to be working fine).


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

Reply
0 Kudos
emmoon1981
Enthusiast
Enthusiast

actually this script has strange behavior

I try it in VMware online lab and see this error but for one hosts its ok but another host didn't change any things so I got confused .

I see more than one error

plz check attached files .

Reply
0 Kudos
emmoon1981
Enthusiast
Enthusiast

Hi Mr LUCD

Is there any chance ?Smiley Sad

Reply
0 Kudos