VMware Cloud Community
rxjoseph
Enthusiast
Enthusiast

How to List VM, Datacenter, Cluster ,VM Host,Hard Disk, Datastore, Canonical Name with Power CLI Script

Hi,

I want to create a CSV file to list the above

I have the following working ok but when I add the "Canonical Name" using the Get-ScsiLun i get an error.

"

Get-ScsiLun : 2/5/2014 3:33:55 PM    Get-ScsiLun        Object reference not set to an instance of an object.

At line:6 char:52

+               ForEach($mylun in ($myds |Get-Scsilun <<<<  |sort-object -property Name)) {

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

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetScsiLun

It is important for me to get both Datastore and canonical name

Can someone please help

This is the script

$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($myHost in ($VM |Get-VMhost |sort-object -property Name)) {

            ForEach($myds in ($VM |Get-datastore |sort-object -property Name)) {

        ForEach($mylun in ($myds |Get-Scsilun |sort-object -property Name)) {   

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

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

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

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

   @{N="VM Host";E={$myHost.name}},

   @{N="Capacity GB";E={$myds.CapacityGB}},

   @{N="Capacity Free GB";E={$myds.FreeSpaceGB}},

          @{N="SCSI";E={$mylun.CanonicalName},

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

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

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

    

      }

    }

  }

}

}

}

}

}

$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "C:\xfer\rj\VmQuery.csv"

Thanks and regards

Joseph

0 Kudos
2 Replies
LucD
Leadership
Leadership

Could it be that you have some "ghost" datastores ?

Does Get-Datastore produce a line for each real datastore, or are there some funny entries in the list ?

Can you eventually check which datastore provokes the error ?

Do something like

Get-Datastore | %{

    "Datastore $($_.Name)"

    $_ | Get-ScsiLun

}


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

0 Kudos
rxjoseph
Enthusiast
Enthusiast

Hi Thanks for the reply.

If I comment ForEach($mylun in ($myds |Get-Scsilun |sort-object -property Name)) {  

The script works Ok. But my requirement is I need to get the Canonical Name for each datastore.

The script still doesn't work

Any suggestions ?

Joseph

0 Kudos