I’m not sure why your xml query isn’t working, but I would like to recommend a cleaner approach to what you are attempting to do. What I’m doing below is converting the XML into a PSObject using Select-Object. Once you have a standard object, it’s a normal for loop to enumerate each node versus using XPath. When you look at your command it’s very long and complicated to see what variable is doing what, so I HIGHLY recommend the use of splatting. Take a look at this:
[xml]$xml = Get-Content C:\Test\test.xml
$vmsToCreate = $xml.SelectNodes("vmwareNEW/newvm") | Select Name, Tmp, VMH, RP, HD, DS, SF, RAM, CPU, PG
foreach ($newVM in $vmsToCreate) {
$newVMParams = @{
Name = $newVM.name
Template = $newVM.Tmp
StorageFormat = $newVM.SF
Datastore = $newVM.DS
VMHost = $newVM.VMH
}
$setVMParams = @{
MemoryGb = $newVM.RAM
NumCPU = $newVM.CPU
Confirm = $false
}
$setHDParams = @{
Datastore = $newVM.DS
StorageFormat = $newVM.SF
CapacityGB = $newVM.HD
Confirm = $false
}
$newHDParams = @{
Datastore = $newVM.DS
StorageFormat = $newVM.SF
Confirm = $false
}
New-VM @newVMParams |
Set-VM @setCMParams |
Get-Harddisk |
Set-Harddisk @setHDParams |
New-Harddisk @newHDParams -CapacityGB = $new.NHD |
New-Harddisk @newHDParams -CapacityGB $_.NNHD |
New-Harddisk @newHDParams -CapacityGB $_.NNNHD |
New-Harddisk @newHDParams -CapacityGB $_.NNNNHD
}
Much cleaner and easier to read. You could also dynamically add and remove parameters
if ($newVM.Name -eq "Foo") {$newVMParams.Add("WhatIf", $true}
Before you test this, you should really consider doing a -WhatIf on your commands step-by-step to ensure the xml properties are mapped properly to the command. So, test New-VM first with WhatIf and slowly step through each phase of your pipeline to validate it’s doing what you expect. You can also just remark out the commands and look at the parameters like so:
[xml]$xml = Get-Content C:\test\test.xml
$vmsToCreate = $xml.SelectNodes("vmwareNEW/newvm") | Select Name, Tmp, VMH, RP, HD, DS, SF, RAM, CPU, PG
foreach ($newVM in $vmsToCreate) {
$newVMParams = @{
Name = $newVM.name
Template = $newVM.Tmp
StorageFormat = $newVM.SF
Datastore = $newVM.DS
VMHost = $newVM.VMH
}
$setVMParams = @{
MemoryGb = $newVM.RAM
NumCPU = $newVM.CPU
Confirm = $false
}
$setHDParams = @{
Datastore = $newVM.DS
StorageFormat = $newVM.SF
CapacityGB = $newVM.HD
Confirm = $false
}
$newHDParams = @{
Datastore = $newVM.DS
StorageFormat = $newVM.SF
Confirm = $false
}
#New-VM @newVMParams |
#Set-VM @setCMParams |
#Get-Harddisk |
#Set-Harddisk @setHDParams |
#New-Harddisk @newHDParams -CapacityGB = $new.NHD |
#New-Harddisk @newHDParams -CapacityGB $_.NNHD |
#New-Harddisk @newHDParams -CapacityGB $_.NNNHD |
#New-Harddisk @newHDParams -CapacityGB $_.NNNNHD
$newVMParams
}