VMware Cloud Community
barnette08
Expert
Expert

New-VM from Template

Hey all,

It's been a while but I'm running into something I'd like another set of eyes on.  I'm running the below script and it executes fine and does what I want but it spits out a lot of errors in the process.  I wanted to clean up some of the errors.  Also, I'm sure there's a much better way to do this, I just reused some old parts I already had.

New-VM : 5/18/2018 8:53:04 AM   New-VM          Operation is not valid due to the current state of the object.

At C:\\PowerCLI\batch_VM_Multi.ps1:25 char:13

+ $vmNewOne = New-VM  -Name tarheels-test-$_ @hshNewVMParams

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

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

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

Get-NetworkAdapter : Cannot validate argument on parameter 'Snapshot'. The argument is null. Provide a valid value for

the argument, and then try running the command again.

At C:\PowerCLI\batch_VM_Multi.ps1:27 char:13

+ $vmNewOne | Get-NetworkAdapter | Set-NetworkAdapter -PortGroup $oSome ...

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

    + CategoryInfo          : InvalidData: (:) [Get-NetworkAdapter], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDev

   ice.GetNetworkAdapter

## Name of Template

$strSourceTemplate = "W2K12R2-STD-Template"

## Destination Cluster

$strSomeCluster = "Court-Side"

## Destination Datastore Cluster

$strDestDStore = "ds-02"

## PortGroup

$strSomePG = "customer-data-201"

$oSomePG = Get-VirtualPortGroup -Name $strSomePG

## Hash Table of Values for for New VM Creation

$hshNewVMParams = @{

    Template = $strSourceTemplate

## default "Resources" resource pool, so as to put VM in desired cluster

    ResourcePool = Get-Cluster $strSomeCluster | Get-ResourcePool -Name "Resources"

Datastore = Get-Datastore -Name $strDestDStore

}

## End Hash Table

1..20 | Foreach {

## Create VM

$vmNewOne = New-VM  -Name tarheels-test-$_ @hshNewVMParams

## Set PortGroup and MAC Address

$vmNewOne | Get-NetworkAdapter | Set-NetworkAdapter -PortGroup $oSomePG -RunAsync

}

1..20 | Foreach {

## Power on each VM

Start-VM -VM tarheels-test-$_

}

0 Kudos
5 Replies
LucD
Leadership
Leadership

That code is quite decent, not a lot to improve.

Are you sure there is no unregistered VM with name sitting on one or more of the datastores?

Have a look with the datastore File Browser.


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

0 Kudos
barnette08
Expert
Expert

There was some junk on the datastore that I cleaned up but it still has an issue running Async.  I'm wondering if it's giving a warning because a clone operation has already kicked off using the same source?  If I remove the RunAsync switch it works fine, but of course is much slower.

0 Kudos
LucD
Leadership
Leadership

There is a maximum of 8 "Non-vMotion provisioning operations per host", or in other words cloning processes.

See page 17 in vSphere 6.5 Configuration Maximums.

Do you start seeing the error after the 8th clone?

I notice you have a look 1 to 20.


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

0 Kudos
barnette08
Expert
Expert

Right, I was thinking it was related to the concurrent operations, but it starts throwing errors after the first task.  Ultimately they are just errors in the PS terminal, as vSphere never actually presents an error.

0 Kudos
LucD
Leadership
Leadership

So does it actually create all 20 VMs?

Aren't there any errors in the vpxd log?


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

0 Kudos