VMware Cloud Community
mark_chuman
Hot Shot
Hot Shot
Jump to solution

Simple VM Report

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
5 Replies
bjm534
Enthusiast
Enthusiast
Jump to solution

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

-Brad
LucD
Leadership
Leadership
Jump to solution

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

mark_chuman
Hot Shot
Hot Shot
Jump to solution

Thanks.  Not sure how to add points in the new system.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
mark_chuman
Hot Shot
Hot Shot
Jump to solution

Very wierd, they were not showing up, but now they are Smiley Happy

0 Kudos