Automation

 View Only
  • 1.  Unable to get all VMs RDM disk information

    Posted Apr 18, 2022 05:53 AM

    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



  • 2.  RE: Unable to get all VMs RDM disk information

    Posted Apr 18, 2022 06:40 AM

    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.



  • 3.  RE: Unable to get all VMs RDM disk information

    Posted Apr 18, 2022 07:41 AM

    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



  • 4.  RE: Unable to get all VMs RDM disk information

    Posted Apr 18, 2022 08:06 AM

    I just explained why you are getting the error.



  • 5.  RE: Unable to get all VMs RDM disk information

    Posted Apr 18, 2022 08:15 AM

    LucD,

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



  • 6.  RE: Unable to get all VMs RDM disk information
    Best Answer

    Posted Apr 18, 2022 09:38 AM

    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
    


  • 7.  RE: Unable to get all VMs RDM disk information

    Posted Apr 18, 2022 10:15 AM

    Thank you very much LucD. That worked