So I currently have a script that has the followning variables
$HDD1 = "20"
$HDD2 = "0"
here is a snipit of the code from my script. $HDD1 and $HDD2 are in addition to the vmdks based off the template I am using. There may be a time when I need one additional disk, another time I may need two additional disks.
If I do not need $HDD2, I would prefer to set it to zero as a way of defining it is not in use.
Right now if I run my script and put $HDD2 = 0 as above. The script completes sucessful and only adds $HDD1 as I would hope, but in powercli it does display errors pertaining to "invalid value passed for SizeGB" it does not like "0" for $HDD2
I am wondering if I should just add this to the the line below pertianing to $HDD2? -ErrorAction SilentlyContinue
Or is there a better way to deal with this?
thank you!!!
$vm | Set-VM -NumCpu ( $cpu ) -Confirm:$false
$vm | New-HardDisk -CapacityGB $HDD1
$vm | New-HardDisk -CapacityGB $HDD2
$vm | Set-VM -Notes $Note -Confirm:$false
Start-VM -VM $vmname
Nothing a Where-clause can't fix
Get-Variable -Name HDD[2-6] | where {$_.Value -ne 0} | %{ $vm | New-HardDisk -CapacityGB $_.Value
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Why don't you do it like this
$vm | Set-VM -NumCpu ( $cpu ) -Confirm:$false
$vm | New-HardDisk -CapacityGB $HDD1
if($HDD2 -ne 0){ $vm | New-HardDisk -CapacityGB $HDD2
} $vm | Set-VM -Notes $Note -Confirm:$false Start-VM -VM $vmname
The ErrorAction would work as well, but any other errors in that line would also be supressed.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I didn't even think of that... thank you
Hi,
would there be an easier way to take it a bit further than:
Thank you!!!
if ($HDD2 -ne 0) {
$vm | New-HardDisk -CapacityGB $HDD2
}
if ($HDD3 -ne 0) {
$vm | New-HardDisk -CapacityGB $HDD3
}
if ($HDD4 -ne 0) {
$vm | New-HardDisk -CapacityGB $HDD4
}
if ($HDD5 -ne 0) {
$vm | New-HardDisk -CapacityGB $HDD5
}
You mean something like this ?
Get-Variable -Name HDD* | %{ $vm | New-HardDisk -CapacityGB $_.Value
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ya, the only thing is I'm not a big fan of the wild card * in scripts so I subsituted that piece.
But the way it is coded it barfs if the value is zero for $HDD. As before I would like it to ignore it is the value is zero.. kind of based like my original question.
Because of the pipes, is it possible?
Get-Variable -Name HDD[2-6] | %{
$vm | New-HardDisk -CapacityGB $_.Value
}
Nothing a Where-clause can't fix
Get-Variable -Name HDD[2-6] | where {$_.Value -ne 0} | %{ $vm | New-HardDisk -CapacityGB $_.Value
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you.. I always forget about the use of those where clauses.