Author Posts

October 13, 2015 at 7:57 am

Hey all, can someone help this old Linux Admin to convert a command to powershell syntax? The part I need help with is the "awk" part at the end. I am just putting in the Linux command to hold the place. Basically I am getting the Name from nslookup and want to strip off "Name:" so that I am left with the FQDN. Example:
Name: and then have only

nslookup "" | Select-String "Name:" | awk '{print $2}'

Eventually I will do a massive nslookup, strip out the FQDN, and use the resulting list of names to perform an admin function remotely on our network.

October 13, 2015 at 8:08 am

((nslookup | Select-String "^name:") -split ":\s+")[1]

October 13, 2015 at 8:08 am

You can accomplish this using -split.

((nslookup "" | Select-String "Name:") -split " +")[1]

The above:
1) Does the nslookup
2) Pipes results to Select-String which uses RegEx to find the string containing "Name:"
3) 1 and 2 are enclosed in parenthesis so that they process before the -split operator takes affect
4) The -split operator uses RegEx to split the string into an array based off of 1 or more space characters. This results in an array with 2 elements.
5) Then we put that in parenthesis so that 1-4 process before we get the value of the element in position 1.

Hope that makes since.

October 13, 2015 at 8:28 am

Bob, Curtis: Thanks. yes it makes perfect since. I failed to tell you I was doing my basic testing at the PS command line so I am getting an "Expressions are only allowed as the first element of a pipeline." I'll try it in a basic script to see if that makes a difference.

October 13, 2015 at 8:30 am

Uhhh... Nevermind. Old habits are hard to break. I was actually, blindly, throwing in a | symbol.

October 13, 2015 at 8:35 am

To be clear a | symbol before the split syntax not the one before Select-String. 🙂