powershell - PowerCLI strange behaviour of variable -
trying collect few properties of vms, reasons entries in output contains info last vm only
the $csv
contains couple of vm names:
vmname centos1 centos2
here code i'm using:
$vmdata = @() $line = '' | select vmname, vmtoolstatus, vmtoolversion, uuid, tag, notes foreach($entry in $csv){ $line.vmname = $entry.vmname $line.vmtoolstatus = (get-vm $entry.vmname).extensiondata.guest.toolsrunningstatus $line.vmtoolversion = (get-vm $entry.vmname).extensiondata.guest.toolsversion $line.uuid = (get-vm $entry.vmname).extensiondata.config.uuid $line.notes = (get-vm $entry.vmname).notes $line.tag = get-vm $entry.vmname | get-tagassignment | select -expandproperty tag | select name $vmdata += $line } $vmdata | export-csv -path c:\report.csv -notypeinformation -force
here csv output - can see both lines contain info vm centos2.
"vmname","vmtoolstatus","vmtoolversion","uuid","tag","notes","startuporder" "centos2","guesttoolsrunning","2147483647","564d7fd7-e58f-e546-ecdf-c347e35cd453",,"test note", "centos2","guesttoolsrunning","2147483647","564d7fd7-e58f-e546-ecdf-c347e35cd453",,"test note",
when debug can see after first cycle $line
updated correct information of vm centos1 , added $vmdata
.
however, when second cycle starts, e.g. after executing line.vmname = $entry.vmname
can see both variables $line
, $vmdata
updated centos2 name.
so, question why $vmdata
gets updated along $line
?
i used piece of code before , worked fine.
i running following version of ps
major minor build revision ----- ----- ----- -------- 5 1 14393 1480
vmware powercli 6.5 release 1 build 4624819
you can fix moving $line = ''
part inside foreach
loop:
$vmdata = @() foreach($entry in $csv){ $line = '' | select vmname, vmtoolstatus, vmtoolversion, uuid, tag, notes $line.vmname = $entry.vmname $line.vmtoolstatus = (get-vm $entry.vmname).extensiondata.guest.toolsrunningstatus $line.vmtoolversion = (get-vm $entry.vmname).extensiondata.guest.toolsversion $line.uuid = (get-vm $entry.vmname).extensiondata.config.uuid $line.notes = (get-vm $entry.vmname).notes $line.tag = get-vm $entry.vmname | get-tagassignment | select -expandproperty tag | select name $vmdata += $line } $vmdata | export-csv -path c:\report.csv -notypeinformation -force
i believe problem occurs because in instance powershell variable acting pointer (a reference type), when update $line
second time affects existing result in $vmdata
.
by moving $line = ''
inside loop reset variable on each iteration doesn't act way.
i recommend instead:
$csv | foreach-object { $props = @{ vmname = $_.vmname vmtoolstatus = (get-vm $_.vmname).extensiondata.guest.toolsrunningstatus vmtoolversion = (get-vm $_.vmname).extensiondata.guest.toolsversion uuid = (get-vm $_.vmname).extensiondata.config.uuid notes = (get-vm $_.vmname).notes tag = (get-vm $_.vmname | get-tagassignment | select -expandproperty tag | select name) } new-object -typename psobject -property $props } | export-csv -path c:\report.csv -notypeinformation -force
this uses hashtable create powershell object within foreach-object loop can pipe output directly export-csv.
Comments
Post a Comment