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.
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
Try something like this:
$name.split(':')[1].split('_')[1]
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.
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.
Maybe trailing spaces. Try $name.trim() to trim leading/trailing spaces
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
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
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