Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 230412

New-VM with -Template switch - Operation is not valid due to the current state of the object

$
0
0

Hi All,

 

I've recently come up against a problem that I haven't been able to resolve. There are a few 'Operation not valid' threads on here and none of them have seemed to help.

 

Basically, when I use the New-VM command in my script, I get the error 'Operation is not valid due to the current state of the object'. This only happens when I use the '-Template' switch.

 

E.g.

New-VM -Name $vmName -VMHost $vmHost <--- works

New-VM -Name $vmName -VMHost $vmHost -Template $template <--- causes the error 'Operation is valid due to the current state of the object'

 

Interestingly, when I run the command on its own and not as part of the script, it runs fine so there's not a syntax error. It might be that there is a much better way to do what I need that doesn't cause the error.

 

The script reads from a CSV and pads out the contents into a full hostname and creates the VM from the correct template.

 

The CSV file looks like this:

VM,Template

SUM01,template1

SUM02,template2

XRG01,template1

GFE01,template2

 

and the script looks like this:

 

Import-CSV $fullPath |`

    ForEach-Object {

       

        $vmName = "XXXX" + $global:siteCodeResult + $_.VM

       

        # If it doesn't exist already then create the custom OS spec which will contain

        # custom VM specific settings

        'Checking for existing Customization Spec...'

        if ((Get-OSCustomizationSpec CustomSpec -ErrorAction SilentlyContinue) -eq $null)

        {

           'Creating new Customization Spec...'

           New-OSCustomizationSpec -OSType Windows -Name CustomSpec -NamingScheme Fixed `

           -NamingPrefix $vmName -FullName Administrator -OrgName TEST -Workgroup WORKGROUP

        } else

        {

           'Modifying Customization Spec...'

           Set-OSCustomizationSpec CustomSpec -NamingPrefix $vmName

        }

       

        # Create VM from template and customise with the specified custom spec

        'Creating VM: ' + $vmName

 

        New-VM -Name $vmName -VMHost $vmHost -Template $_.Template     

    }

 

Hopefully that all made sense!

 

Any help is greatly appreciated,
Matt


Viewing all articles
Browse latest Browse all 230412

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>