Hi, this following command executes ok and is successful but I receive the error/warning below . I know it's crude but it works ok and I'd just like to clean it up with a little help if possible.
Can anyone advise/spot the problem please?
Many thanks in advance and kind regards
Steve
Command
foreach($Name in $newVmList){
$taskTab[ (New-VM -Name $Name -Template $modelVm -ResourcePool $ResourcePool -Datastore (Get-DatastoreCluster -Name $dscluster) -OSCustomizationSpec $customspec -whatif -RunAsync).Id] = $Name
}
ERROR:
Index operation failed; the array index evaluated to null.
At C:\tmp\newvm02.ps1:35 char:10
+ $taskTab[ <<<< (New-VM -Name $Name -Template $modelVm -ResourcePool $ResourcePool -Datastore (Get-DatastoreCluster -Name
$dscluster) -OSCustomizationSpec $customspec -whatif -RunAsync).Id] = $Name
+ CategoryInfo : InvalidOperation: (System.Collections.Hashtable:Hashtable) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
I notice you still have square brackets in there, the Add method only needs parenthesis.
It uses 2 parameters; the key and the value.
And get rid of the WhatIf switch, otherwise there will be no ID.
Like this
$taskTab.Add((New-VM -Name $Name -Template $modelVm -ResourcePool $ResourcePool -Datastore (Get-DatastoreCluster -Name $dscluster) -OSCustomizationSpec $customspec -RunAsync).Id,$Name)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I suspect the WhatIf switch on your New-VM cmdlet might be causing this.
With that switch no object is returned, it just shows on the console what the cmdlet would do without actually doing it.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi, thanks for the info but I get the same message regardless if I include the -whatif switch.
Thanks
Steve
Where did you define the $taskTab table ?
Did you define it as a hash table ?
If yes, try changing the code to
$taskTab.Add((New-VM -Name $Name -Template $modelVm -ResourcePool $ResourcePool -Datastore (Get-DatastoreCluster -Name $dscluster) -OSCustomizationSpec $customspec -RunAsync).Id,$Name)
The Add method on a hash table allows you add a new entry.
The 1st parameter is the key, the 2nd the value.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi, you'll have to excuse my ignorance on this one! I'm v new to PowerCLI
It's defined in the following line only I believe?:
$newVmList = "srdelete1","srdelete2"
$taskTab = @{}
Thanks
Steve
That is correct.
Does the Add method work ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry - forgot to mention on last post it didn't help. Same error.
Thanks
Steve
Are you sure about that ?
There isn't any indexing done (no square brackets), but it should use the Add method on the hash table.
Would it be possible to show the command and the error ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi, see below for command and error. Bear in mind this command does actually work but I get this error.
Thanks again!
# Create all the VMs specified in $newVmList
foreach($Name in $newVmList){
$taskTab.Add[(New-VM -Name $Name -Template $modelVm -ResourcePool $ResourcePool -Datastore (Get-DatastoreCluster -Name $dscluster) -OSCustomizationSpec $customspec -whatif -RunAsync).Id] = $Name
}
----------------------------
Index operation failed; the array index evaluated to null.
At C:\tmp\newvm02.ps1:35 char:14
+ $taskTab.Add[ <<<< (New-VM -Name $Name -Template $modelVm -ResourcePool $ResourcePool -Datastore (Get-DatastoreCluster -Name $dscluster)
-OSCustomizationSpec $customspec -whatif -RunAsync).Id] = $Name
+ CategoryInfo : InvalidOperation: (System.Void Add...m.Object value):PSMethod) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex
I notice you still have square brackets in there, the Add method only needs parenthesis.
It uses 2 parameters; the key and the value.
And get rid of the WhatIf switch, otherwise there will be no ID.
Like this
$taskTab.Add((New-VM -Name $Name -Template $modelVm -ResourcePool $ResourcePool -Datastore (Get-DatastoreCluster -Name $dscluster) -OSCustomizationSpec $customspec -RunAsync).Id,$Name)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks again for your assistance. No more errors.
Steve