0

I'd like to use powershell to get the server name from a string in which the format is the same every time. I create a text file using a command in HP's Data Protector. Examples of strings are...

   host="ts-sve-serverT.ca.mycompany.com"  
   host="ts-sve-serverG.ca.mycompany.com"  
   host="ts-sve-serverA.ca.mycompany.com"  

There are 3 spaces at the beginning. I'm interested in extracting the server name after the first set of quotes and before the first period. The server name may contain 0 to 2 dashes.

2 Answers 2

1
$serverNames = gc 'c:\logfile.txt' |?{ $_ -match '"([^\.]*)\.' } |%{ $matches[1] }
Sign up to request clarification or add additional context in comments.

Comments

0

Either use the -match operator, as latkin suggested, or the Select-String cmdlet:

$names = gc 'C:\path\to\your.log' | Select-String '"(.*?)\.' | % {
  $_.Matches.Groups[1].Value
}

The above requires PowerShell v3. On earlier versions you'll have to expand the matches like this:

$names = gc 'C:\path\to\your.log' | Select-String '"(.*?)\.' `
  | select -Expand Matches `
  | select -Expand Groups `
  | select -Last 1 Value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.