Automation

 View Only
  • 1.  Simple VM Report

    Posted May 07, 2013 11:56 AM

    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



  • 2.  RE: Simple VM Report

    Posted May 07, 2013 01:50 PM

    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



  • 3.  RE: Simple VM Report

    Posted May 07, 2013 03:29 PM

    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.



  • 4.  RE: Simple VM Report

    Posted May 08, 2013 04:57 PM

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



  • 5.  RE: Simple VM Report
    Best Answer

    Posted May 08, 2013 05:38 PM

    I think it's as simple as clicking one of the Helpful Answer or Correct Answer buttons at the bottom of a reply :smileygrin:



  • 6.  RE: Simple VM Report

    Posted May 08, 2013 05:42 PM

    Very wierd, they were not showing up, but now they are :smileyhappy: