Hello,
I'm trying to get a script to convert multiple machines to templates. If I convert just one machine at a time it works, but when I get multiple I get errors thrown at me and a machine may or may not convert to a template. Below are the errors:
Select multiple VM's to convert to templates
VM --> PShellTest rsupdtest2 rsupdtest is powered on, will power off the VM
VM --> PShellTest rsupdtest2 rsupdtest is powered on, will power off the VM
Shutdown-VMGuest : 9/24/2018 2:49:35 PM Stop-VMGuest Operation "Shutdown VM guest." failed for VM "rsupdtest2" for
the following reason: The attempted operation cannot be performed in the current state (Powered off).
At C:\Users\<nameremoved>\Desktop\GenScripts\PowerOffVMConvertToTemplateBulk.ps1:31 char:1
+ Shutdown-VMGuest -VM $VMname.Name -Verbose:$false -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Stop-VMGuest], VimException
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_ShutdownVmGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.
Commands.StopVmGuest
Shutdown-VMGuest : 9/24/2018 2:49:35 PM Stop-VMGuest Operation "Shutdown VM guest." failed for VM "rsupdtest" for the
following reason: The attempted operation cannot be performed in the current state (Powered off).
At C:\Users\<nameremoved>\Desktop\GenScripts\PowerOffVMConvertToTemplateBulk.ps1:31 char:1
+ Shutdown-VMGuest -VM $VMname.Name -Verbose:$false -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Stop-VMGuest], VimException
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_ShutdownVmGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.
Commands.StopVmGuest
Shutdown-VMGuest : 9/24/2018 2:49:59 PM Stop-VMGuest Could not find VirtualMachine with name 'PShellTest'.
At C:\Users\<nameremoved>\Desktop\GenScripts\PowerOffVMConvertToTemplateBulk.ps1:31 char:1
+ Shutdown-VMGuest -VM $VMname.Name -Verbose:$false -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (PShellTest:String) [Stop-VMGuest], VimException
+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdle
ts.Commands.StopVmGuest
Shutdown-VMGuest : 9/24/2018 2:49:59 PM Stop-VMGuest Operation "Shutdown VM guest." failed for VM "rsupdtest2" for
the following reason: The attempted operation cannot be performed in the current state (Powered off).
At C:\Users\<nameremoved>\Desktop\GenScripts\PowerOffVMConvertToTemplateBulk.ps1:31 char:1
+ Shutdown-VMGuest -VM $VMname.Name -Verbose:$false -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Stop-VMGuest], VimException
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_ShutdownVmGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.
Commands.StopVmGuest
Shutdown-VMGuest : 9/24/2018 2:49:59 PM Stop-VMGuest Operation "Shutdown VM guest." failed for VM "rsupdtest" for the
following reason: The attempted operation cannot be performed in the current state (Powered off).
At C:\Users\<nameremoved>\Desktop\GenScripts\PowerOffVMConvertToTemplateBulk.ps1:31 char:1
+ Shutdown-VMGuest -VM $VMname.Name -Verbose:$false -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Stop-VMGuest], VimException
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_ShutdownVmGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.
Commands.StopVmGuest
VM --> PShellTest rsupdtest2 rsupdtest is powered on, will power off the VM
Shutdown-VMGuest : 9/24/2018 2:50:24 PM Stop-VMGuest Could not find VirtualMachine with name 'PShellTest'.
At C:\Users\<nameremoved>\Desktop\GenScripts\PowerOffVMConvertToTemplateBulk.ps1:31 char:1
+ Shutdown-VMGuest -VM $VMname.Name -Verbose:$false -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (PShellTest:String) [Stop-VMGuest], VimException
+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdle
ts.Commands.StopVmGuest
Shutdown-VMGuest : 9/24/2018 2:50:24 PM Stop-VMGuest Could not find VirtualMachine with name 'rsupdtest'.
At C:\Users\<nameremoved>\Desktop\GenScripts\PowerOffVMConvertToTemplateBulk.ps1:31 char:1
+ Shutdown-VMGuest -VM $VMname.Name -Verbose:$false -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (rsupdtest:String) [Stop-VMGuest], VimException
+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdle
ts.Commands.StopVmGuest
Shutdown-VMGuest : 9/24/2018 2:50:24 PM Stop-VMGuest Operation "Shutdown VM guest." failed for VM "rsupdtest2" for
the following reason: The attempted operation cannot be performed in the current state (Powered off).
At C:\Users\<nameremoved>\Desktop\GenScripts\PowerOffVMConvertToTemplateBulk.ps1:31 char:1
+ Shutdown-VMGuest -VM $VMname.Name -Verbose:$false -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Stop-VMGuest], VimException
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_ShutdownVmGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.
Commands.StopVmGuest
else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\<nameremoved>\Desktop\GenScripts\PowerOffVMConvertToTemplateBulk.ps1:37 char:1
+ else{
+ ~~~~
+ CategoryInfo : ObjectNotFound: (else:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Below is the code:
Get-Datacenter | select Name | Format-Table #Lists available datacenters
$mydatacenter = Read-Host "Choose a datacenter"
Get-Folder -Location $mydatacenter -Type VM | sort Name | Format-Table #Lists all VM folders in the datacenter
$myfolder = Read-Host "Choose a folder"
Write-Host "Select multiple VM's to convert to templates"
$vmstopoweroff = Get-Folder -Name $myfolder -Type VM | Get-VM | sort Name | Out-Gridview -OutPutMode Multiple #List all VMs in the VM folder only
$VMname = Get-VM -Name $vmstopoweroff | Select Name, @{N="Powerstate";E={($_).powerstate}} #Checking power state#
foreach ($vm in $vmstopoweroff){
if ($VMname.Powerstate -like "PoweredOn") {
Write-Host "VM -->",$VMname.Name," is powered on, will power off the VM"
Shutdown-VMGuest -VM $VMname.Name -Verbose:$false -Confirm:$false
}
}
Sleep 60
$template = Set-VM -ToTemplate $vm -Confirm:$false #Converts VM to template
The other thing I was thinking of just now to add to the script is a note that can be put in vSphere that states when the template was created so we have a marker so if a VM is needed again or we need to delete it we know how long it's been.