I've sure, this is a newb question, but why does this code
Out-Log "Generating ESXi host report..."
#showESXihost
Import-Csv $csvfile | Foreach { Get-VM $_.Name | Select Name, @{N="ESX Host";E={Get-VMHost -VM $_}}}
Out-Log "Generating UsedSpaceGB report..."
#showGBused
Import-Csv $csvfile | Foreach { Get-VM $_.Name | Select Name, @{n="SpaceUsedGB"; e={[math]::round( $_.UsedSpaceGB )}}}
out-log "Generating source datastore report..."
#showdatastore.ps1
Import-Csv $csvfile | Foreach { Get-VM $_.Name | Select Name, @{N="Datastore";E={$_ | Get-Datastore}}}
Return (note missing header and values for 2nd and 3rd queries) :
Generating ESXi host report...
Name ESX Host
---- --------
sanssnte01vee71 sanssnte01esx06.xyz.com
sanssnte01sma02 sanssnte01esx04.xyz.com
sanssnto01vmw18 sanssnte01esx04.xyz.com
Generating UsedSpaceGB report...
sanssnte01vee71
sanssnte01sma02
sanssnto01vmw18
Generating source datastore report...
sanssnte01vee71
sanssnte01sma02
sanssnto01vmw18
If I reorder the queries, the first one is still the only one to return correctly
thanks!
Try affixing " | ft -Autosize" at the end of each line. This should format the output nicely.
Did you already try replacing the Foreach statements by Foreach-Object cmdlets?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Same result using foreach-object - do I need to clear some variable or between the reports to get the next one to work?
Out-Log "Generating ESXi host report..."
#showESXihost
Import-Csv $csvfile | Foreach-object { Get-VM $_.Name | Select Name, @{N="ESX Host";E={Get-VMHost -VM $_}}}
Out-Log "Generating UsedSpaceGB report..."
#showGBused
Import-Csv $csvfile | Foreach-object { Get-VM $_.Name | Select Name, @{n="SpaceUsedGB"; e={[math]::round( $_.UsedSpaceGB )}}}
out-log "Generating source datastore report..."
#showdatastore.ps1
Import-Csv $csvfile | Foreach-object { Get-VM $_.Name | Select Name, @{N="Datastore";E={$_ | Get-Datastore}}}
thanks
If you run your commands as a script, you should output all of the columns in the first row, If you modify the first Import-CSV command into the following, it will work:
Import-Csv $csvfile | Foreach { Get-VM $_.Name | Select Name, @{N="ESX Host";E={Get-VMHost -VM $_}},SpaceUsedGB,Datastore }
If you do the Import-CSV only once, your script will run faster. For example, you can use the following code, stripped down to the relevant part:
$VMs = Import-Csv $csvfile | Foreach { Get-VM $_.Name }
$VMs | Select Name, @{N="ESX Host";E={Get-VMHost -VM $_}},SpaceUsedGB,Datastore
$VMs | Select Name, @{n="SpaceUsedGB"; e={[math]::round( $_.UsedSpaceGB )}}
$VMs | Select Name, @{N="Datastore";E={$_ | Get-Datastore}}
yeah - that will work:
Name ESX Host SpaceUsedGB Datastore
---- -------- ----------- ---------
sanssnte01vee71 sanssnte01esx06.xyz...
sanssnte01sma02 sanssnte01esx04.xyz...
sanssnto01vmw18 sanssnte01esx04.xyz...
sanssnte01vee71 13
sanssnte01sma02 9
sanssnto01vmw18 8
sanssnte01vee71 SANEMC02-TE01-T3-data01
sanssnte01sma02 SANEMC02_NL01_T3_Offline-S...
sanssnto01vmw18 SANEMC02-TE01-T2-data01
But as a report its not readable - why don't the sequential foreach queries return the expected results
If they are run isolated they do, but why not one after another in the same script ? (be gentle, I'm a newb)
thanks
Try affixing " | ft -Autosize" at the end of each line. This should format the output nicely.
Yes, that format is more readable, thanks!