VMware Cloud Community
Azasadny
Contributor
Contributor
Jump to solution

Retreive a VM working Directory Via PowerCli

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.

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
5 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Azasadny
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Azasadny
Contributor
Contributor
Jump to solution

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

Thx

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference