Trim

Guys.

I want to obtain a list of server names from the the VMware Power CLI.  Using Get-VM will return the name of each server. Lets say for the purpose of this post that they are;

XX0001 - Test Server
XX0002 - Test Server

The Windows NetBIOS name is XX0001

So to export the list i thought i would use the Trim command as i would like to ignore all text after the six character. our server names are all six characters in length.

So the code i have so far is below. I am thinking Trim is not the right command to use but maybe im missing something. Trim is not something i play around with much.

-----------

$Text = Get-VM | select-object Name

$Text.TrimStart()

July 6th, 2015 7:08am

TrimStart() will only remove leading spaces. You could use split - 

get-vm | Select Name | % {($_.Name -split " ")[0]}



  • Edited by Braham20 19 hours 53 minutes ago
  • Marked as answer by WallaceTech 19 hours 43 minutes ago
Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 7:17am

Hi,

Use this to keep 6 first characters

$Text.Substring(0,6)

July 6th, 2015 7:17am

Hi,

there are many ways to achieve this, here is one:

$a = 'XX0001 - Test Server'

$a.Substring(0,6)


Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 7:19am

Thanks very much for the reply. Your method seems to do the trick. The other examples for some reason dont want to play with the VMware CLI. However what you have provided has worked and im just picking it apart now to understand it.

Thanks very much.

Also thank you to everyone who took the time to reply.

July 6th, 2015 7:30am

To use substring it would still need to be repeated on every machine in a loop - 

get-vm | Select Name | % {$_.name.substring(0,6)}

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 7:41am

Get-VM | select @{N='NetBIOSName';E={($_.Name).Substring(0,6)}}

or

Get-VM | select @{N='NetBIOSName';E={($_.Name)[0..5] -join ''}}

July 6th, 2015 7:46am

TrimStart() will only remove leading spaces. You could use split - 

get-vm | Select Name | % {($_.Name -split " ")[0]}



  • Edited by Braham20 Monday, July 06, 2015 11:16 AM
  • Marked as answer by WallaceTech Monday, July 06, 2015 11:26 AM
Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 11:15am

Get-VM | select @{N='NetBIOSName';E={($_.Name).Substring(0,6)}}

or

Get-VM | select @{N='NetBIOSName';E={($_.Name)[0..5] -join ''}}

July 6th, 2015 11:44am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics