VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to get all VMs RDM disk information

Hi,

I am unable to get the output of all VMs which has RDM disk information in a single file from the below script

$vmview = Get-View -ViewType VirtualMachine
$DiskInfo = @()

foreach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | where { $_.DeviceInfo.Label -match "SCSI Controller" }))
{
foreach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | where { $_.ControllerKey -eq $VirtualSCSIController.Key -and $_.Backing -is [VMware.Vim.VirtualDiskRawDiskMappingVer1BackingInfo] }))
{
$VirtualDisk = "" | Select VM_Host, VM, LUN, SCSI_Control, SCSI_ID, Capacity_GB, Disk_Name, Disk_File, Canonical_Name, Console_Name, Disk_Type, Vendor, Model #, Policy
$VirtualDisk.VM = $vmview.name
$esx = Get-View -Id $vmview.Runtime.Host
$VirtualDisk.VM_Host = ($esx).Name
$VirtualDisk.SCSI_Control = $VirtualSCSIController.DeviceInfo.Label
$VirtualDisk.Disk_Name = $VirtualDiskDevice.DeviceInfo.Label
$VirtualDisk.Disk_Type = $VirtualDiskDevice.backing
$VirtualDisk.SCSI_ID = $VirtualDiskDevice.UnitNumber
$VirtualDisk.Disk_File = $VirtualDiskDevice.Backing.FileName
$VirtualDisk.Capacity_GB = $VirtualDiskDevice.CapacityInKB * 1KB / 1GB
$VirtualDisk.Canonical_Name = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).CanonicalName
$lun = Get-ScsiLun -VmHost ($esx).Name -CanonicalName $VirtualDisk.Canonical_Name
$VirtualDisk.LUN = $lun.RuntimeName.SubString($lun.RuntimeName.LastIndexof("L") + 1)
#$VirtualDisk.Console_Name = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).DevicePath
$VirtualDisk.Console_Name = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).DeviceName
$VirtualDisk.Vendor = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).Vendor
$VirtualDisk.Model = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).Model

$DiskInfo += $VirtualDisk
}
}

$DiskInfo | Export-Csv -Path $out_csvpath -Delimiter "," -NoTypeInformation

 

Error

Cannot find an overload for "SubString" and the argument count: "1".
At D:\All_Export_RDM_node.ps1:49 char:4
+ $VirtualDisk.LUN = $lun.RuntimeName.SubString($lun.RuntimeName.Las ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You just need another foreach loop.

$DiskInfo = @()
Get-View -ViewType VirtualMachine -PipelineVariable VMView |
ForEach-Object -Process {
  foreach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | Where-Object { $_.DeviceInfo.Label -match "SCSI Controller" })) {
    foreach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | Where-Object { $_.ControllerKey -eq $VirtualSCSIController.Key -and $_.Backing -is [VMware.Vim.VirtualDiskRawDiskMappingVer1BackingInfo] })) {
      $VirtualDisk = "" | Select-Object VM_Host, VM, LUN, SCSI_Control, SCSI_ID, Capacity_GB, Disk_Name, Disk_File, Canonical_Name, Console_Name, Disk_Type, Vendor, Model #, Policy
      $VirtualDisk.VM = $vmview.name
      $esx = Get-View -Id $vmview.Runtime.Host
      $VirtualDisk.VM_Host = ($esx).Name
      $VirtualDisk.SCSI_Control = $VirtualSCSIController.DeviceInfo.Label
      $VirtualDisk.Disk_Name = $VirtualDiskDevice.DeviceInfo.Label
      $VirtualDisk.Disk_Type = $VirtualDiskDevice.backing
      $VirtualDisk.SCSI_ID = $VirtualDiskDevice.UnitNumber
      $VirtualDisk.Disk_File = $VirtualDiskDevice.Backing.FileName
      $VirtualDisk.Capacity_GB = $VirtualDiskDevice.CapacityInKB * 1KB / 1GB
      $VirtualDisk.Canonical_Name = ($esx.Config.StorageDevice.ScsiLun | Where-Object { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).CanonicalName
      $lun = Get-ScsiLun -VmHost ($esx).Name -CanonicalName $VirtualDisk.Canonical_Name
      $VirtualDisk.LUN = $lun.RuntimeName.SubString($lun.RuntimeName.LastIndexof("L") + 1)
      #$VirtualDisk.Console_Name = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).DevicePath
      $VirtualDisk.Console_Name = ($esx.Config.StorageDevice.ScsiLun | Where-Object { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).DeviceName
      $VirtualDisk.Vendor = ($esx.Config.StorageDevice.ScsiLun | Where-Object { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).Vendor
      $VirtualDisk.Model = ($esx.Config.StorageDevice.ScsiLun | Where-Object { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).Model

      $DiskInfo += $VirtualDisk
    }
  }
}

$DiskInfo | Export-Csv -Path $out_csvpath -Delimiter "," -NoTypeInformation


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Where is this code coming from?

That will never work.
The $vmView variable contains all your VMs, hence $esx contains an array of the ESXi nodes where you have VMs running.
This means that $VirtualDisk.Canonical_Name also will contain an array, and the Substring method does not work on an array of strings.

You can't just randomly throw together some code, create a thread and then suppose someone to fix it.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

The below code works for one VM. I would like to get the RDM details for all the VMs hence I modified as above, hence I was getting that error

$vmview = Get-View -ViewType VirtualMachine -Filter @{'Name' = MyDB1 }
$DiskInfo = @()

foreach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | where { $_.DeviceInfo.Label -match "SCSI Controller" }))
{
foreach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | where { $_.ControllerKey -eq $VirtualSCSIController.Key -and $_.Backing -is [VMware.Vim.VirtualDiskRawDiskMappingVer1BackingInfo] }))
{
$VirtualDisk = "" | Select VM_Host, VM, LUN, SCSI_Control, SCSI_ID, Capacity_GB, Disk_Name, Disk_File, Canonical_Name, Console_Name, Disk_Type, Vendor, Model #, Policy
$VirtualDisk.VM = $vmview.name
$esx = Get-View -Id $vmview.Runtime.Host
$VirtualDisk.VM_Host = ($esx).Name
$VirtualDisk.SCSI_Control = $VirtualSCSIController.DeviceInfo.Label
$VirtualDisk.Disk_Name = $VirtualDiskDevice.DeviceInfo.Label
$VirtualDisk.Disk_Type = $VirtualDiskDevice.backing
$VirtualDisk.SCSI_ID = $VirtualDiskDevice.UnitNumber
$VirtualDisk.Disk_File = $VirtualDiskDevice.Backing.FileName
$VirtualDisk.Capacity_GB = $VirtualDiskDevice.CapacityInKB * 1KB / 1GB
$VirtualDisk.Canonical_Name = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).CanonicalName
$lun = Get-ScsiLun -VmHost ($esx).Name -CanonicalName $VirtualDisk.Canonical_Name
$VirtualDisk.LUN = $lun.RuntimeName.SubString($lun.RuntimeName.LastIndexof("L") + 1)
#$VirtualDisk.Console_Name = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).DevicePath
$VirtualDisk.Console_Name = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).DeviceName
$VirtualDisk.Vendor = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).Vendor
$VirtualDisk.Model = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).Model

$DiskInfo += $VirtualDisk
}
}

$DiskInfo | Export-Csv -Path $out_csvpath -Delimiter "," -NoTypeInformation

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I just explained why you are getting the error.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

Please suggest, any other way, I can get the details of all VMs ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You just need another foreach loop.

$DiskInfo = @()
Get-View -ViewType VirtualMachine -PipelineVariable VMView |
ForEach-Object -Process {
  foreach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | Where-Object { $_.DeviceInfo.Label -match "SCSI Controller" })) {
    foreach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | Where-Object { $_.ControllerKey -eq $VirtualSCSIController.Key -and $_.Backing -is [VMware.Vim.VirtualDiskRawDiskMappingVer1BackingInfo] })) {
      $VirtualDisk = "" | Select-Object VM_Host, VM, LUN, SCSI_Control, SCSI_ID, Capacity_GB, Disk_Name, Disk_File, Canonical_Name, Console_Name, Disk_Type, Vendor, Model #, Policy
      $VirtualDisk.VM = $vmview.name
      $esx = Get-View -Id $vmview.Runtime.Host
      $VirtualDisk.VM_Host = ($esx).Name
      $VirtualDisk.SCSI_Control = $VirtualSCSIController.DeviceInfo.Label
      $VirtualDisk.Disk_Name = $VirtualDiskDevice.DeviceInfo.Label
      $VirtualDisk.Disk_Type = $VirtualDiskDevice.backing
      $VirtualDisk.SCSI_ID = $VirtualDiskDevice.UnitNumber
      $VirtualDisk.Disk_File = $VirtualDiskDevice.Backing.FileName
      $VirtualDisk.Capacity_GB = $VirtualDiskDevice.CapacityInKB * 1KB / 1GB
      $VirtualDisk.Canonical_Name = ($esx.Config.StorageDevice.ScsiLun | Where-Object { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).CanonicalName
      $lun = Get-ScsiLun -VmHost ($esx).Name -CanonicalName $VirtualDisk.Canonical_Name
      $VirtualDisk.LUN = $lun.RuntimeName.SubString($lun.RuntimeName.LastIndexof("L") + 1)
      #$VirtualDisk.Console_Name = ($esx.Config.StorageDevice.ScsiLun | where { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).DevicePath
      $VirtualDisk.Console_Name = ($esx.Config.StorageDevice.ScsiLun | Where-Object { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).DeviceName
      $VirtualDisk.Vendor = ($esx.Config.StorageDevice.ScsiLun | Where-Object { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).Vendor
      $VirtualDisk.Model = ($esx.Config.StorageDevice.ScsiLun | Where-Object { $_.Uuid -eq $VirtualDiskDevice.Backing.LunUuid }).Model

      $DiskInfo += $VirtualDisk
    }
  }
}

$DiskInfo | Export-Csv -Path $out_csvpath -Delimiter "," -NoTypeInformation


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much LucD. That worked 🙂

Reply
0 Kudos