Automation

 View Only
  • 1.  Trying to get script to convert multiple machines to templates

    Posted Sep 25, 2018 04:59 PM

    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.



  • 2.  RE: Trying to get script to convert multiple machines to templates
    Best Answer

    Posted Sep 25, 2018 05:34 PM

    Most of these errors are due to the fact that you are trying to power off VMs that are already powered off.

    The error mentioning 'else' at the end must come from another version of your script, since there is no else in the version you posted.

    Try with this version

    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


    foreach ($vm in (Get-VM -Name $vmstopoweroff)) {

      if ($VM.Powerstate -like "PoweredOn") {

       Write-Host "VM --> $($vm.Name)is powered on, will power off the VM"

      Shutdown-VMGuest -VM $vm -Verbose:$false -Confirm:$false

      }

      Sleep 60

      $template = Set-VM -ToTemplate $vm -Confirm:$false #Converts VM to template

    }

    Be aware that this will rely on the fact that a VM is actully powered off after 60 seconds.

    A better way would be to do that in a loop.

    Also note that this way, the selected VMs will be converted one by one, no parallelism.

    That 'note' you want to add, could that be a Custom Attribute on the VM?



  • 3.  RE: Trying to get script to convert multiple machines to templates

    Posted Sep 25, 2018 10:12 PM

    Hi LucD,

    The new script works.  I added in the ability to put in notes to show in vSphere.  Here's the final bit of 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

    $date = Get-Date
    $NewNotes = "Converted to template on $date"

    foreach ($vm in (Get-VM -Name $vmstopoweroff)) {

      if ($VM.Powerstate -like "PoweredOn") {

       Write-Host "VM --> $($vm.Name) is powered on, will power off the VM"

      Shutdown-VMGuest -VM $vm -Verbose:$false -Confirm:$false

      }

      Sleep 30

      $template = Set-VM -ToTemplate $vm -Confirm:$false #Converts VM to template
      Set-VM  $vm -Notes $newNotes -Confirm:$false  #Adds note to template with date/time it was converted

    }