VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Trim first and last characters in the output

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.

 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could use a Where-method

$s.Where{$_ -ne ''} | %{$_.Substring(22,24)}


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

You could use the SubString method

$s = 'vml.0200ae0000624a9370c3776b2dac9a4f49005afa95466c61736841'
$s.Substring(22,24)

 


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could use a Where-method

$s.Where{$_ -ne ''} | %{$_.Substring(22,24)}


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

That worked perfectly. Thank you very much 🙂

0 Kudos