0

I am running a SQL query and need to pull a machine name out of the field. The results have multiple entries separated by semicolons. I only care about the value for Server=PCName\SQL_Instance. The values I'm working with look like...

Provider=SQLOLEDB;Server=PCName\SQL_Instance;SomeOtherCrap=moreWords;ThisGoes=OnAndOn
Proer=SQLOLEDB;Server=PCName\SQL_Instance;SsdgsdfomeOtherCrap=moreWords;TzfdzdfghisGoes=OnAndOn

The server= will always contain my PC name (local Access DB install) and they will all be the same even though there re multiple lines. I really only need to get the name once. And while I already split the values by the ; my brain is too fried to figure out how to get the server name from the results. I'm sure this is painfully obvious as the value is handed to you on a platter but I'm not that smart to even be able to figure out how to search the answer.

1 Answers1

0

You can use StartsWith("Server") to filter out the required object, then throw in another split on "=" and "/", probably easier with RegEx, but I like splits e.g.:

 $string = "Provider=SQLOLEDB;Server=PCName\SQL_Instance;SomeOtherCrap=moreWords;ThisGoes=OnAndOn"
 $string.Split(";") | Where {$_.startswith("Server")}
 Server=PCName\SQL_Instance
 ($string.Split(";") | Where {$_.startswith("Server")}).split("=")[1].split("\")[0]
 PCName
andr3yk
  • 176
  • 6
  • I was getting there in a much uglier way... I had something along the lines of `$string.Split(";") $string | ForEach-Object { if ($_.startswith("Server=")){ $string=$_ }}` This, of course, STILL leaving me to get rid of the crap before and after. Is there any way to get only the first result? the way I did it gives me a single record but only because it already went through it 5 times and just saved the last one. All instances of this are going to be the same. – n00b2Everything Jan 24 '21 at 01:51
  • 1
    `($string.Split(";") | Where {$_.startswith("Server")}).split("=")[1].split("\")[0]` is the way to go. I like one liners because I sometime have to pass these to cmd. Thanks again. – n00b2Everything Jan 24 '21 at 01:57