I have used this script for some time to deploy VMs from template. However after upgrading our environment from esxi 6.7 to 7.0 and upgrading powercli on my workstation to the latest version this script started trowing error messages. My VMs still get created as expected but they need to be started manually and the progress bar is not working any more.
Here is a simplified version of the script (I have removed all unnecessary lines of code):
param (
[Parameter(Mandatory=$true)]
[string[]]$newVMList
)
$template = "test"
$osCust = Get-OSCustomizationSpec -name "Template"
$Folder = Get-Folder -Id Folder-group-v148095
$resPool = Get-ResourcePool -ID ResourcePool-resgroup-186282
$taskTab = @{}
foreach($Name in $newVmList){
$taskTab[(New-VM -name $Name -ResourcePool $resPool -Location $folder -Datastore DatastoreName -Template $templaten -OSCustomizationSpec $osCust -RunAsync).Id] = $Name
}
# Start each VM that is completed
$runningTasks = $taskTab.Count
while($runningTasks -gt 0){
Get-Task | % {
if($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success"){
Get-VM $taskTab[$_.Id] | Start-VM
$taskTab.Remove($_.Id)
$runningTasks--
}
elseif($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error"){
$taskTab.Remove($_.Id)
$runningTasks--
}
}
Start-Sleep -Seconds 15
}
and the error message is:
New-VM : 20/09/2022 09:18:18 New-VM One or more errors occurred.
At Y:\Work\deploy-vms.ps1:13 char:11
+ $taskTab[(New-VM -name $Name -ResourcePool $resPool -Location $folder ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-VM], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM
Index operation failed; the array index evaluated to null.
At Y:\Work\deploy-vms.ps1:13 char:1
+ $taskTab[(New-VM -name $Name -ResourcePool $resPool -Location $folder ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
I am not sure how to fix it. Can someone point me in the right direction please.
Thanks
Did you check if the Id property (on the object returned by New-VM) contains a value?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
