Contributor
Contributor

script to gather VM' vmdk path and datastore canonical name

Hi

Can somebody please help with a script that I am working on I am trying to gather all VM's and the datastores on which their hard drive reside on along with the canonical name of the datastore

this is what i have at the moment but keep getting errors

$VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {

  ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {

    ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {

      ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {

      ForEach ($datastore in ($Cluster | get-vmhost | get-datastore | Get-ScsiLun -LunType disk |select -Property CanonicalName)){

        "" | Select-Object -Property @{N="VM";E={$VM.Name}},

          @{N="Datacenter";E={$Datacenter.name}},

          @{N="Cluster";E={$Cluster.Name}},

          @{N="Hard Disk";E={$HardDisk.Name}},

          @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},

   @{N="CononicalName";E={$datastore.CononicalName}}

          @{N="VMDKpath";E={$HardDisk.FileName}}

      }

    }

  }

}

}

$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "c:\vmdkPaths.csv"

Error

Get-ScsiLun : 28/08/2013 9:57:30 AM    Get-ScsiLun        ScsiLun info is available only for Vmfs datstores.

At C:\Users\jsmith484\Documents\PowershellScripts\ICEVMDKReport3.ps1:6 char:68

+       forEach ($datastore in ($Cluster | get-vmhost | get-datastore | Get-ScsiLun - ...

+                                                                       ~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-ScsiLun], VimException

    + FullyQualifiedErrorId : Client20_StorageServiceImpl_GetScsiLun_InvalidDatastoreType,VMware.VimAutomation.ViCore.Cmdlets.Commands.Hos

   t.GetScsiLun

I think its something to do with the way that I am gathering the canonical name using the get-scsilun?

0 Kudos
2 Replies
Leadership
Leadership

I don't have access to a PowerCLI console right now but I think that you have to change

| get-datastore | Get-ScsiLun -LunType disk

into

| get-datastore | Where-Object {$_.Type -eq 'VMFS'} | Get-ScsiLun -LunType disk

This will filter the output of the Get-Datastore cmdlet and will send only VMFS datastores to the Get-ScsiLun cmdlet.

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

Hi I am trying to get the same query working

Did you get this fixed if so can you post the script ?

I need this very urgently for a datastore project

Thanks

Joseph

0 Kudos