Home > Blockchain >  Trimming powershell objects
Trimming powershell objects

Time:11-24

I am using the powershell following command

Get-RdsSessionHost -TenantName "api" -HostPoolName "vvt"|Select-Object SessionHostName 

and the results I get is
wvdpd24.api.com.au
wvdpd25.api.com.au
wvdpd1.api.com.au

now I want to remove the part ".api.com.au"
How do i do this, any idea guys?

CodePudding user response:

You can use -replace operator. It has the following syntax.

<input> -replace <regular-expression>, <substitute>

In this case the <regular-expression> should be ".api.com.au$"(As we want to match api.com.au at the end of the string). So your final code should look like this,

Get-RdsSessionHost -TenantName "api" -HostPoolName "vvt" |Select-Object SessionHostName | ForEach-Object {$_.SessionHostName -replace "\.api\.com\.au$", ""}

CodePudding user response:

There are different ways of doing this of course.

Using -split:

(Get-RdsSessionHost -TenantName "api" -HostPoolName "vvt").SessionHostName | ForEach-Object { ($_ -split '\.')[0] }

Using -replace

(Get-RdsSessionHost -TenantName "api" -HostPoolName "vvt").SessionHostName | ForEach-Object { $_ -replace '\.api\.com\.au$' }

or

(Get-RdsSessionHost -TenantName "api" -HostPoolName "vvt").SessionHostName | ForEach-Object { $_ -replace '\.\w \.\w \.\w $' }

or

(Get-RdsSessionHost -TenantName "api" -HostPoolName "vvt").SessionHostName | ForEach-Object { ($_ -replace '^([^\.] ).*', '$1') }

Where the above uses regex operators -split and -replace, you could also use String methods where no escaping needs to be done:

(Get-RdsSessionHost -TenantName "api" -HostPoolName "vvt").SessionHostName | ForEach-Object { $_.Split(".")[0] }

or

(Get-RdsSessionHost -TenantName "api" -HostPoolName "vvt").SessionHostName | ForEach-Object { $_.Replace(".api.com.au","") }
  • Related