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-$_
}