For some reason this script is only reporting on the last vm in the csv. Most likely my incorrect use of the first "foreach". Any help would be appreciated.
$curvc = read-host -prompt "Enter VC Name"
connect-viserver -server $curvc
$vms = Import-CSV .\name_dev.csv
foreach ($vm in $vms) {Get-VM $vm.Name |
foreach {
$Report = "" | Select-Object -property Name,NumCpu,MemoryMB,Host
$Report.Name = $_.Name
$Report.NumCpu = $_.NumCpu
$Report.MemoryMB = $_.MemoryMB
$Report.Host = $_.VMHost
Write-Output $Report
} | Export-Csv ".\VM.csv"
}
disconnect-viserver * -confirm:$false
I think it's as simple as clicking one of the Helpful Answer or Correct Answer buttons at the bottom of a reply :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Mark,
I believe we might be over complicating this. You can use the command set below to get what you are looking for;
****************************************
#collect vcenter name, user, password. Delete the $esxuser and $esxpass if you want it to prompt for creds
$curvc = read-host "Enter VC Name"
$esxuser = read-host "Enter VC username"
$esxpass = read-host "Enter VC password"
#connects to vcenter. If you want the prompt for creds delete everything after the $curvc
connect-viserver -Server $curvc -User $esxuser -Password $esxpass
#gets list of VM's and selects the Name,NumCpu, MemoryMB,and VMhost properties
get-vm | select-object Name,NumCpu,MemoryMB,VMHost | export-csv C:\vm\vms.csv
#disconnects from anything that powershell is connected to
Disconnect-VIServer -Server $global:DefaultVIServers -Force -Confirm:$false
*********************************************
-Brad
The placement of the Export-Csv is not correct.
$curvc = read-host -prompt "Enter VC Name"
connect-viserver -server $curvc
$vms = Import-CSV .\name_dev.csv
&{foreach ($vm in $vms) {
Get-VM $vm.Name | foreach {
$Report = "" | Select-Object -property Name,NumCpu,MemoryMB,Host
$Report.Name = $_.Name
$Report.NumCpu = $_.NumCpu
$Report.MemoryMB = $_.MemoryMB
$Report.Host = $_.VMHost
$Report
}
}} | Export-Csv ".\VM.csv"
disconnect-viserver * -confirm:$false
The trick with the Call operator (&) is required because of the ForEach loop, that statement doesn't place anything in the pipeline.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks. Not sure how to add points in the new system.
I think it's as simple as clicking one of the Helpful Answer or Correct Answer buttons at the bottom of a reply :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Very wierd, they were not showing up, but now they are