Now I can take server names by assigning variable for every 3rd row, but I understand that this is a hard way to get final result
From my script you can see that I’m new in scripting, but I hope you can give me some advises how to get specific words as objects from the text. I only found how to make whole string as object.
Thank you in advance!
You are pretty much on the right track with what you have. What is causing you a bit of a headache is those silly quote marks. If you change your first line to this
then you will get some nice consistent characters to work with.
$file2 = $file -split ‘-host “‘
would then be able to be done with -split '"' and give you a much nicer array to work with. The nicest way to split would be using a regular expression, but as you are just starting into scripting you want to avoid those dark arts.
You also need to process each line in turn which a foreach loop will do nicely.
So that will process each line in turn, split the data on the normal quote marks that we put in and create an array. The first element of that array is -host. The second is the computer name itself. [1] will get the second element in the array ([0] gets the first).
Oddly, I couldn’t find a ready-made generic tokenizer for .NET or PowerShell (which makes me want to write one soon). However, since your text file’s syntax basically matches a PowerShell command line’s quoting rules, we can cheat and use the PSParser class:
Edit: I was assuming that the “smart quotes” in your post came from either the forum software or Microsoft Word or something. If they’re actually in your data file, then it might throw off the PSParser as well.