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
emmoon1981
Enthusiast
Enthusiast

so I just try your script  and Didn't change anything .

there is strange behavior !! as you told me I  Create hosts.txt and host01,host02,host03,...,hostn.txt like this and put all .txt files at the same directory "c:\"

in my office host01 is esx1.edkolhamedan.org  and host02 is esx1.shabomosa.org and .. so I have hosts.txt and "esx1.edkolhamedan.org.txt" and "esx1.shabomosa.org.txt" and ...

hosts.txt            host01.txt                                  host02.txt                                      host03.txt

host01                shobad2012.host01.org          shobad2012.host02.org                shobad.host03.org

host02                shobapp.host01.org                Shobapp.host02.org                      shobapp.host03.org

host03                Shobdb.host01.org                  shobdb.host02.org                        shobdb.host03.org

.                          .

.

.

hostn

0 Kudos
LucD
Leadership
Leadership

My script assumes that the names in hostx.txt files contain the displaynames of the VMs.

Is for example 'shobad2012.host01.org' a Displayname ?

Does Get-VM -Name shobad2012.host01.org return anything ?


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

0 Kudos
emmoon1981
Enthusiast
Enthusiast

Yes 'shobad2012.host01.org' is  a Displayname.

I will check the "Get-VM -Name shobad2012.host01.org " cmdlet.

0 Kudos
emmoon1981
Enthusiast
Enthusiast

I check the "Get-VM -Name shobad2012.host01.org

it is somethings like this

PowerCLI C:\> get-vm -name shobad2012.shabomosa.org

Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
ShobAD2012.ShAbom... PoweredOn  4        4.000

0 Kudos
LucD
Leadership
Leadership

I put some debugging info in the script.

Can you try this one ?

$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" | %{

        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

}

  


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

0 Kudos
emmoon1981
Enthusiast
Enthusiast

I try and see this error

Name                           Port  User
----                           ----  ----
esx1.edkolhamedan.org          443   root
Looking at VM EDKolAD2012.EDHamedan.org, will have order# 1
Get-VM : A positional parameter cannot be found that accepts argument '-'.
At line:17 char:15
+         $vm = Get-VM - -Name $_ -ErrorAction SilentlyContinue
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-VM], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

Looking at VM EDKolDb.EdHamedan.org, will have order# 1
Get-VM : A positional parameter cannot be found that accepts argument '-'.
At line:17 char:15
+         $vm = Get-VM - -Name $_ -ErrorAction SilentlyContinue
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-VM], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

Looking at VM EDKolApp.EDHamedan.org, will have order# 1
Get-VM : A positional parameter cannot be found that accepts argument '-'.
At line:17 char:15
+         $vm = Get-VM - -Name $_ -ErrorAction SilentlyContinue
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-VM], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

Looking at VM EDFS.EdKolHamedan.org, will have order# 1
Get-VM : A positional parameter cannot be found that accepts argument '-'.
At line:17 char:15
+         $vm = Get-VM - -Name $_ -ErrorAction SilentlyContinue
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-VM], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

Looking at VM EDKolDB2.EDHamedan.org, will have order# 1
Get-VM : A positional parameter cannot be found that accepts argument '-'.
At line:17 char:15
+         $vm = Get-VM - -Name $_ -ErrorAction SilentlyContinue
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-VM], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

Looking at VM EDKolWSUS.EDHamedan.org, will have order# 1
Get-VM : A positional parameter cannot be found that accepts argument '-'.
At line:17 char:15
+         $vm = Get-VM - -Name $_ -ErrorAction SilentlyContinue
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-VM], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

Looking at VM Firewall.EdHamedan.org, will have order# 1
Get-VM : A positional parameter cannot be found that accepts argument '-'.
At line:17 char:15
+         $vm = Get-VM - -Name $_ -ErrorAction SilentlyContinue
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-VM], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

Looking at VM WHDB.EdKolHamedan.Org, will have order# 1
Get-VM : A positional parameter cannot be found that accepts argument '-'.
At line:17 char:15
+         $vm = Get-VM - -Name $_ -ErrorAction SilentlyContinue
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-VM], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

PowerCLI C:\>

0 Kudos
LucD
Leadership
Leadership

Oops, there was a typo in there, I corrected it.

Please try again


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

0 Kudos
emmoon1981
Enthusiast
Enthusiast

I try your last script and see the error in txt file attached.

all vm names are true and I checked them but

its just work for one of the hosts and it has  strange behaviorSmiley Sad

0 Kudos
emmoon1981
Enthusiast
Enthusiast

Did you see the last error log ? :smileyblush:

0 Kudos
LucD
Leadership
Leadership

It looks as if there might be a blank line at the end of the file(s) with the VM names.


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

0 Kudos
emmoon1981
Enthusiast
Enthusiast

Hi Mr Lucd   

Beleive  me  my file is ok  and  no blank  at  the end of the VM names files

Plz help me 

0 Kudos
LucD
Leadership
Leadership

The last error messages you attached seem to indicate that there is a 10th line with no VMName on there.

Hence my guess that it could be a blank line.

Can you check, and eventually attach the current copy of the file with the VM names you are using.


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

0 Kudos
emmoon1981
Enthusiast
Enthusiast

Yes   I  wil send for you  VMs file and check   again

0 Kudos
emmoon1981
Enthusiast
Enthusiast

Hi   these are my File that attached .

thanks for every things.

0 Kudos
LucD
Leadership
Leadership

I could find no way in which to get the error you forwarded me.

error1.png

without introducing an empty line in the text file

error2.png

When I use that as input I can repeat the error you were seeing (I obviously left out the actual Get-VM)

error3.png

With the input files you attached, I can't recreate the error.

Can you ?


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

0 Kudos
emmoon1981
Enthusiast
Enthusiast

For  each  one of  those  files  such "esx1.edkolhamedan.org.txt"

I see  that  erorr and  as  you  told me  about the  blank  line  in  myvm.txt   files  I  recreate  that  file  I  mean "esx1.edkolhamedan.org" and check to be  without any  blank  lines so again  I  saw that error and  nothing  changed   I  mean  for  somethings  like  this  photo  I  attached

0 Kudos
LucD
Leadership
Leadership

Than I'm lost Smiley Sad

One more try before I give up. Can you run the following ?

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

foreach ($singleViserver in $vihosts){

    Write-Host "Reading file C:\$($singleViserver).txt"

    $lineNr = 1

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

        Write-Host "Line $($lineNr) contains $($_)"

        $lineNr++

    }

}


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

0 Kudos
emmoon1981
Enthusiast
Enthusiast

I try it .

0 Kudos
LucD
Leadership
Leadership

That is ok.

Now when you run the last version of the script, you are still getting that error ?

If yes, then I'm lost


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

0 Kudos
emmoon1981
Enthusiast
Enthusiast

Yes  and  nothings  changed :smileyconfused:

0 Kudos