VMware Cloud Community
mhorgan61
Contributor
Contributor

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

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

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership

From where do you run the script ? From the PowerCLI prompt ? Or from an editor ?

Did you try restarting the prompt/editor ?


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

mhorgan61
Contributor
Contributor

Hi LucD,

I have been using Powershell ISE to develop the script because only a small part of it calls upon PowerCLI. Interestingly though, when using the PowerCLI prompt it actually runs fine without errors from what I can see.

So if I right click and run the script with PowerShell it will fail but if I call it directly from PowerCLI it works. Any idea why this might be?

Thanks,

Matt

Reply
0 Kudos
MKguy
Virtuoso
Virtuoso

Can you make sure the script is being executed in a 32bit Powershell environment? I suppose it's calling the default 64bit Powershell executable when you run the script from like that.

New-VM and a few other cmdlets don't work with a 64bit Powershell and sometimes throw weird errors like these.

You can put that into your script for debugging: http://stackoverflow.com/a/8588982

VMware vSphere PowerCLI 5.1 Release 2 Release Notes:

The following PowerCLI features are supported only on the 32-bit version of Windows PowerShell.

    New-OSCustomizationSpec and Set-OSCustomizationSpec

    New-VM and Set-VM (only when used for applying customization specifications)

When running against vCenter Server or ESX/ESXi versions earlier than 5.0, the following PowerCLI features are supported only on the 32-bit version of Windows PowerShell.

    Invoke-VMScript

    Copy-VMGuestFile

    New-VMGuestRoute, Get-VMGuestRoute, and Remove-VMGuestRoute

    Get-VMGuestNetworkInterface and Set-VMGuestNetworkInterface

    Set-HardDisk (only when used for resizing guest disk partitions)

-- http://alpacapowered.wordpress.com
Reply
0 Kudos
mhorgan61
Contributor
Contributor

Hi MKguy,

I'm running it all in 32bit for that reason. If I run it in 64bit then it then the script will stop as soon as it gets to the customization spec command.

For now I can do what I need to do by running the script directly from the PowerCLI prompt so it isn't the end of the world.

Thanks,

Matt

Reply
0 Kudos
LucD
Leadership
Leadership

I noticed the same behavior (runs from PowerCLI prompt but not from a GUI), but I don't have an explanation for it.

Could be the threading mode (STA or MTA), but I didn't succeed in finding any 100% proof for that.

You could, just for the sake of it, also try to run it from the PowerShell prompt (after you load the PSSnapin of course).

Should work as well in my experience.


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

guyrleech
Virtuoso
Virtuoso

Interesting that this still seems to be a problem in 2020 with vSphere 7.0. I have been using the same PowerShell 5.1 ISE session over a couple of days to develop a script that calls New-VM and today it just started throwing this exception every time, not having changed the New-VM parameters. Restarted the ISE session and it stopped throwing the error.

-- If you found this or any other answer useful please consider the use of the Helpful or Correct buttons to award points.
Reply
0 Kudos