Hi
Can anyone explain the following ...
C:\> $vms = Get-VM
C:\> $vms
C:\> foreach ( $vm in $vms ) { Write-Host "Test $vm" }
Test
C:\> Get-PowerCLIVersion
PowerCLI Version
-
VMware vSphere PowerCLI 4.1 build 264274
Connected to an ESXi 4.1 hosts as root, there are no VMs so why does the loop execute once? I know that to get the above there are easier methods but for other reasons I need to perform a loop, this was just a test to prove what is to me a phenomenon.
Any help appreciated.
Thanks, Andy.
Message was edited by: avarcher
I believe this is due to PowerShell assigning a value of null to $vm. If you want to avoid going through that you can test $vm -eq $Null before starting ForEach, or you could use $vm = @(Get-VM).
Dave
VMware Communities User Moderator
Now available - vSphere Quick Start Guide
Do you have a system or PCI card working with VMDirectPath? Submit your specs to the Unofficial VMDirectPath HCL.
I believe this is due to PowerShell assigning a value of null to $vm. If you want to avoid going through that you can test $vm -eq $Null before starting ForEach, or you could use $vm = @(Get-VM).
Dave
VMware Communities User Moderator
Now available - vSphere Quick Start Guide
Do you have a system or PCI card working with VMDirectPath? Submit your specs to the Unofficial VMDirectPath HCL.
Hi Dave
You are spot on ...
C:\> $vms = Get-VM
C:\> $vms -eq $null
True
C:\> $vms = @(Get-VM)
C:\> $vms -eq $null
C:\> foreach ( $vm in $vms ) { Write-Host "Test $vm" }
C:\>
I've now read about @(...) and I'm sorry that I'm none the wiser (Holmes Powershell pocket ref, Hal Rottenberg), your answer gets me on my way but I'd appreciate if you could help me understand why.
Many thanks, Andy.
$null is treated as a scalar in powershell, thats why.
You might want to read my blog article about this behaviour.
http://www.van-lieshout.com/2009/12/null-or-nothing/
Arnim