VMware Cloud Community
nlong12
Enthusiast
Enthusiast
Jump to solution

Script to stop vm's in an array create snapshots and restart vm's

Created the following script to stop vm's create snapshots and restart vm's.  Been looking at help and not sure the following is the best way to accomplish my goal of first shutting down the vm's once they have been shutdown create the snapshots and once the the snapshots have been created restart the vms.   Any suggestions would be greatly appreciated

Thank you

Norm

# scom_shutdown_snapshot_vms

#

# Description:

#  Script shuts down guest OS and subsequently creates snapshots

#  

#

# Command line syntax

#  D:\PowerShell_Production\powercli_prod\scom_shutdown_snapshot_vms

#

# Run Notes

# None

#

# Dependencies

#   Powershell v5 or greater

#   VMware PowerCLI 6.5 or greater

#

# Change log:

#    08/05/19 NLong - Script Creation

#

# Last updated:

#   08/05/19 NLong - Script Creation

#

# Begin Script

#Builds an array of machine names in the $VMs variable.

$vms = @("App211","Obadiah","App238")

foreach ($vm in $vms)

{

#By adding RunAsync it will execute the shutdowns all in succession.

#Get-VM $vm | Stop-VMGuest -RunAsync -Confirm:$true 

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

}

#Create SCOM snapshots

$vm = Foreach ($vm in $vms)

{

New-Snapshot -VM $vm -Name “PRE SCOM UPGRADE” -RunAsync -Description “Created by Nlong Prior to SCOM UPGRADE ” -Confirm:$false

}

#Start SCOM VM's after snapshots have been created

foreach ($vm in $vms)

{

#By adding RunAsync it will execute the startup all in succession.

Start-VM -VM $vm -RunAsync -Confirm:$false

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

My bad, I had the operands for -contains in the wrong order.

It should be

while($vms.PowerState -contains 'PoweredOn'){

  sleep 2

   $vms = Get-VM -Name $vmNames

}

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

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

To make sure the VMs are actually shut down before doing the snapshot, I would add a loop that tests the powerstate of the VMs.

This certainly required when you decide to add the RunAsync switch on the Stop-VMGuest cmdlet.

# scom_shutdown_snapshot_vms

#

# Description:

# Script shuts down guest OS and subsequently creates snapshots

#

# Command line syntax

# D:\PowerShell_Production\powercli_prod\scom_shutdown_snapshot_vms

#

# Run Notes

# None

#

# Dependencies

# Powershell v5 or greater

# VMware PowerCLI 6.5 or greater

#

# Change log:

# 08/05/19 NLong - Script Creation

#

# Last updated:

# 08/05/19 NLong - Script Creation

#


# Begin Script


#Builds an array of machine names in the $VMs variable.

$vmNames = @("App211", "Obadiah", "App238")

$vms = Get-VM -Name $vmNames


foreach ($vm in $vms) {

   #By adding RunAsync it will execute the shutdowns all in succession.

   #Get-VM $vm | Stop-VMGuest -RunAsync -Confirm:$true

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

}


# Wait till all VMs are shut down

while($vms.PowerState -contains 'PoweredOn'){

  sleep 2

   $vms = Get-VM -Name $vmNames

}


#Create SCOM snapshots

$vm = Foreach ($vm in $vms) {

   New-Snapshot -VM $vm -Name “PRE SCOM UPGRADE” -RunAsync -Description “Created by Nlong Prior to SCOM UPGRADE ” -Confirm:$false

}


#Start SCOM VM's after snapshots have been created

foreach ($vm in $vms) {

   #By adding RunAsync it will execute the startup all in succession.

   Start-VM -VM $vm -RunAsync -Confirm:$false

}


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

0 Kudos
nlong12
Enthusiast
Enthusiast
Jump to solution

Hello Lucd;

Running the script as follows:  (with no -runasync)

$vmNames = @("FWApp211", "Obadiah")

$vms = Get-VM -Name $vmNames

foreach ($vm in $vms) {

   #By adding RunAsync it will execute the shutdowns all in succession.

   #Get-VM $vm | Stop-VMGuest -RunAsync -Confirm:$true

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

}

# Wait till all VMs are shut down

while('PoweredOn' -contains $vms.PowerState){

  sleep 2

   $vms = Get-VM -Name $vmNames

}

#Create SCOM snapshots

$vm = Foreach ($vm in $vms) {

   New-Snapshot -VM $vm -Name “PRE SCOM UPGRADE”  -Description “Created by Nlong Prior to SCOM UPGRADE ” -Confirm:$false

}

#Start SCOM VM's after snapshots have been created

foreach ($vm in $vms) {

   #By adding RunAsync it will execute the startup all in succession.

   Start-VM -VM $vm  -Confirm:$false

}

I get the following following error message:

tate          IPAddress            OSFullName                                   

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

Running        {172.18.11.42, fe... CentOS 6 (64-bit)                            

Running        {172.18.11.85}       Red Hat Enterprise Linux 7 (64-bit)          

New-Snapshot : 8/6/2019 9:28:50 AM New-Snapshot Server task failed: The operation is not allowed in the current state.

At line:45 char:4

+    New-Snapshot -VM $vm -Name “PRE SCOM UPGRADE”  -Description “Creat ...

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

    + CategoryInfo          : NotSpecified: (:) [New-Snapshot], VimException

    + FullyQualifiedErrorId : Security_Impl_TaskResultConverter_TaskNotSucceeded,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewSnapshot

New-Snapshot : 8/6/2019 9:28:50 AM New-Snapshot Server task failed: The operation is not allowed in the current state.

At line:45 char:4

+    New-Snapshot -VM $vm -Name “PRE SCOM UPGRADE”  -Description “Creat ...

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

    + CategoryInfo          : NotSpecified: (:) [New-Snapshot], VimException

    + FullyQualifiedErrorId : Security_Impl_TaskResultConverter_TaskNotSucceeded,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewSnapshot

Start-VM : 8/6/2019 9:28:51 AM Start-VM Server task failed: The attempted operation cannot be performed in the current state (Powered on).

At line:51 char:4

+    Start-VM -VM $vm  -Confirm:$false

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

    + CategoryInfo          : NotSpecified: (:) [Start-VM], VimException

    + FullyQualifiedErrorId : Security_Impl_TaskResultConverter_TaskNotSucceeded,VMware.VimAutomation.ViCore.Cmdlets.Commands.StartVM

Start-VM : 8/6/2019 9:28:52 AM Start-VM Server task failed: The attempted operation cannot be performed in the current state (Powered on).

At line:51 char:4

+    Start-VM -VM $vm  -Confirm:$false

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

    + CategoryInfo          : NotSpecified: (:) [Start-VM], VimException

    + FullyQualifiedErrorId : Security_Impl_TaskResultConverter_TaskNotSucceeded,VMware.VimAutomation.ViCore.Cmdlets.Commands.StartVM

Trying to get this to work before my 1pm SCOM upgrade

Thank you for your help!

Norm

0 Kudos
nlong12
Enthusiast
Enthusiast
Jump to solution

Hello Lucd,

If I change the following -Confirm:$true  , making sure the vm's are down then the rest of the script works, not sure the while loop is working

foreach ($vm in $vms) {

   #By adding RunAsync it will execute the shutdowns all in succession.

   #Get-VM $vm | Stop-VMGuest -RunAsync -Confirm:$true

   Stop-VMGuest -VM $vm -Confirm:$true

}

Hope this helps you to help me.

Norm

0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, I had the operands for -contains in the wrong order.

It should be

while($vms.PowerState -contains 'PoweredOn'){

  sleep 2

   $vms = Get-VM -Name $vmNames

}

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

Was it helpful? Let us know by completing this short survey here.


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

0 Kudos
nlong12
Enthusiast
Enthusiast
Jump to solution

Hello Lucd,

Thank you for the correction, the script now works!!

Norm

0 Kudos