8 Replies Latest reply on Aug 6, 2018 4:21 AM by LucD

    Deploy VM from template fails

    alfwro Lurker

      I'm trying to automate vm from template deployment, here is my script:

       

      param (

      [Parameter(Mandatory=$true)]

      [string]$vmName,

      [Parameter(Mandatory=$true)]

      [string]$ipAddress

      )

       

      Get-OSCustomizationSpec -name "W7_cust" | New-OSCustomizationSpec -name tempcust -type NonPersistent

       

      Get-OSCustomizationSpec -name tempcust | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $ipAddress -SubnetMask 255.255.255.0 -DefaultGateway 10.0.0.254 -dns 10.0.0.50

       

      $osCust = Get-OSCustomizationSpec -Name tempcust

       

      new-vm -name $vmName -ResourcePool Test -Template template_name -Datastore stor01 -Folder "Folder" -OSCustomizationSpec $osCust

       

       

      I know the script is fine because I can run it line by line and it executes OK however when I run it as above in a form of a script it fails with this error:

       

      new-vm : 06/08/2018 10:42:20    New-VM          Operation is not valid due to the current state of the object.

      At G:\deploy-training-VM.ps1:14 char:1

      + new-vm -name $vmName -ResourcePool Test -Template template_name  ...

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

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

          + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

       

      Why does it fail? How do I get it working?