Automation

 View Only
  • 1.  Trim first and last characters in the output

    Posted Dec 29, 2020 09:06 AM

    Hi,

    I would like to trim or ignore certain characters from the output, Please help

    $DiskInfo= @()
    foreach ($VMview in Get-VM JIVVPSHRSQL12 | Get-View){
    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})) {
    $VirtualDisk = "" | Select VMname, SCSIController, DiskName, SCSI_ID, DeviceName, DiskFile, DiskSize
    $VirtualDisk.VMname = $VMview.Name
    $VirtualDisk.SCSIController = $VirtualSCSIController.DeviceInfo.Label
    $VirtualDisk.DiskName = $VirtualDiskDevice.DeviceInfo.Label
    $VirtualDisk.SCSI_ID = "$($VirtualSCSIController.BusNumber) : $($VirtualDiskDevice.UnitNumber)"
    $VirtualDisk.DeviceName = $VirtualDiskDevice.Backing.DeviceName
    $VirtualDisk.DiskFile = $VirtualDiskDevice.Backing.FileName
    $VirtualDisk.DiskSize = $VirtualDiskDevice.CapacityInKB * 1KB / 1GB
    $DiskInfo += $VirtualDisk
    }}}
    $DiskInfo | ft -auto

    From the Device name, I would like to trim or ignore first 22 characters and last 12 characters as I would like to get only the text which is marked in bold below

    vml.0200ae0000624a9370c3776b2dac9a4f49005afa95466c61736841

    Please help.

     



  • 2.  RE: Trim first and last characters in the output

    Posted Dec 29, 2020 09:16 AM

    You could use the SubString method

     



  • 3.  RE: Trim first and last characters in the output

    Posted Dec 29, 2020 09:23 AM

    That was perfect LucD,

    But I am getting the below error for few of the disks, how can I suppress these errors ?

    You cannot call a method on a null-valued expression.
    At D:\Get_Harddisk_Info.ps1:11 char:1
    + $VirtualDisk.DeviceName = $VirtualDiskDevice.Backing.DeviceName.subst ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull



  • 4.  RE: Trim first and last characters in the output
    Best Answer

    Posted Dec 29, 2020 09:32 AM

    You could use a Where-method



  • 5.  RE: Trim first and last characters in the output

    Posted Dec 29, 2020 11:48 AM

    That worked perfectly. Thank you very much