VMware Cloud Community
admin
Immortal
Immortal
Jump to solution

Strange script output format

I am having an issue I can't quite figure out. I am running a command that returns a string like this:
Server Name     : VM_TEST1
All I need to get is the value after the VM_, that is the actual server name. VM_ just designates it as a virtual machine. So I am trying to get just TEST1. I can do so as follows


$name_temp = $name | Select-String -pattern 'Server Name' | Out-String | foreach {$_.split(":")[-1]}

I am left with: VM_VMTEST1 So I remove the preceeding whitespace and the VM_

$name=$name_temp.Remove(0,4)

Now I am left with just the name, however for some reason when I run the script is outputs like this:

TEST1

TEST2

TEST3


I do not understand why it is putting three lines in between each server name.  This happens if I run it from the cli, powergui or even exporting to file.

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The problem you have comes from the combination of the Select-String and Out-String cmdlets. The Select-String cmdlet doesn't return a string but instead returns a Microsoft.PowerShell.Commands.MatchInfo object. For example:

IgnoreCase : True

LineNumber : 1

Line : Server Name : VM_TEST1

Filename : InputStream

Path : InputStream

Pattern : Server Name

Context :

Matches : {Server Name}

If you use the Out-String cmdlet to make a string of this object, it adds the empty lines.

As you can see the original line is in the Line property of this object. We can use this Line property in our code. Like this:

$name_temp = $name | Select-String -Pattern 'Server Name' | Foreach-Object {$_.Line.split('_')[1]}

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
7 Replies
avlieshout
VMware Employee
VMware Employee
Jump to solution

Try something like this:

$name.split(':')[1].split('_')[1]

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
admin
Immortal
Immortal
Jump to solution

I am still seeing the blank lines in the output. Also some of the server names contain underscores so if I do a split on it it messes up the names.

0 Kudos
admin
Immortal
Immortal
Jump to solution

Another thing I have noticed. The $name is a string and if I do a $name.length the value returned it greater the actual length of every server by 6. So if my server is name Test, the $name.length is 10. Quite confused.

0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

Maybe trailing spaces. Try $name.trim() to trim leading/trailing spaces

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The problem you have comes from the combination of the Select-String and Out-String cmdlets. The Select-String cmdlet doesn't return a string but instead returns a Microsoft.PowerShell.Commands.MatchInfo object. For example:

IgnoreCase : True

LineNumber : 1

Line : Server Name : VM_TEST1

Filename : InputStream

Path : InputStream

Pattern : Server Name

Context :

Matches : {Server Name}

If you use the Out-String cmdlet to make a string of this object, it adds the empty lines.

As you can see the original line is in the Line property of this object. We can use this Line property in our code. Like this:

$name_temp = $name | Select-String -Pattern 'Server Name' | Foreach-Object {$_.Line.split('_')[1]}

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
admin
Immortal
Immortal
Jump to solution

That was it, and thanks for the explaination as I am trying to get a better understanding what I am doing. Would you happen to know why the Out-string adds the extra lines? Is it just the default dehavior? It would be nice to know why then maybe I will eliminate a future mistake.

Thanks again

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You used the Out-String cmdlet without the -Stream parameter. In this case the cmdlet returns only one string. If you use the -Stream parameter the cmdlet returns a string for every record in the input. If you do that the empty lines will be removed.

I must say that I don't see any usecase for the Out-String cmdlet in PowerCLI. Every cmdlet that I regurlarly use returns PowerShell objects and takes objects as input. So there is in PowerCLI no need to convert objects to strings. And no need to use the Out-String cmdlet.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos