Automation

 View Only
  • 1.  Retreive a VM working Directory Via PowerCli

    Posted Jun 24, 2010 08:37 PM

    Does any one know if it is possible to query the location of working directory of a VM via PowerCli?

    I'm looking to sort VM's based on the datastores their working directories reside on.



  • 2.  RE: Retreive a VM working Directory Via PowerCli

    Posted Jun 24, 2010 08:58 PM

    You can use the next script to retrieve the working directories of all the virtual machines sorted by directory. If a machine has harddisks on multiple datastores the script shows them all.

    Get-VM | ForEach-Object {
      $VM = $_
      $VM.HardDisks | ForEach-Object {
        $HardDisk = $_
        $Report = "" | select-Object Directory,VM
        $Report.Directory = $HardDisk.FileName.Split("/")[0]
        $Report.VM = $VM.Name
        $Report
      }
    } | Sort-Object -property Directory -Unique
    

    Because the forum software has problems with square brackets, and square brackets are used in the script, I attach the script to this post also.

    Regards, Robert



  • 3.  RE: Retreive a VM working Directory Via PowerCli

    Posted Jun 24, 2010 09:06 PM

    How could i exclude the secondary disks on the other datastores?



  • 4.  RE: Retreive a VM working Directory Via PowerCli
    Best Answer

    Posted Jun 24, 2010 09:20 PM

    I assume that the primairy harddisk always has the name "Hard disk 1". To retrieve only the primary harddisks and exclude the other ones we select only the harddisks with name "Hard disk 1".

    Get-VM | ForEach-Object {
      $VM = $_
      $VM.HardDisks | `
        Where-Object {$_.Name -eq "Hard disk 1"} | `
        ForEach-Object {
          $HardDisk = $_
          $Report = "" | select-Object Directory,VM
          $Report.Directory = $HardDisk.FileName.Split("/")[0]
          $Report.VM = $VM.Name
          $Report
        }
    } | Sort-Object -property Directory -Unique
    



  • 5.  RE: Retreive a VM working Directory Via PowerCli

    Posted Jun 28, 2010 04:55 PM

    One last question, How would I export the results to csv? I've tried export-csv but ir errors out.

    Thx



  • 6.  RE: Retreive a VM working Directory Via PowerCli

    Posted Jun 28, 2010 05:19 PM

    Try like this

    $report = @()
    Get-VM | ForEach-Object {
      $VM = $_
      $VM.HardDisks | `
        Where-Object {$_.Name -eq "Hard disk 1"} | `
        ForEach-Object {
          $HardDisk = $_
          $row = "" | select-Object Directory,VM
          $row.Directory = $HardDisk.FileName.Split("/")[0]
          $row.VM = $VM.Name
          $Report += $row
        }
    }
    $report | Export-Csv "C:\MyReport.csv" -NoTypeInformation
    

    ____________

    Blog: LucD notes

    Twitter: lucd22