0

I am sure this is obvious but for the life of me I can not find the information. Many postings on splitting output to multiple lines, but I can't find information on using Multiple lines as input.

I often create text files with lists of variables then use $servers=GC servers.txt and use a "For each" loop to process them. There has to be a way to just include that list in the script.

For example if I normally create a 'servers.txt' consisting of:

server1

server2

server3....

Is it possible to list those server in the script it self. Something like (and I know this doesn't work as writen:

$servers= @(
    Server1
    Server2
    Server3
    )

UPDATE

I know I could separate them in to quotes and add commas but that is specifically what I am trying to avoid. If I copy a list of servers from a spread sheet with right click copy, I'd like to be able to paste it in my script without having to add commas and single quotes. Right now I avoid this by dumping the contents in to a text file then use Get-Content to import it, but I am trying to find a way to bypass that extra step and just be able to paste it in the script then click run without having to alter the text.

2
  • You are very close with your example. Enclose the values inside $servers in quotes and seperate them with a comma (so it'll be @("server1","server2") etc). You can also use enters/returns to make it more readable. Commented Feb 14, 2020 at 18:05
  • gc which is an alias for Get-Content by default reads in a line of text as an array item. To create an equivalent, you just need to use array creation syntax --> $servers = 'server1','server2','server3'. The real question would be how is servers.txt getting populated and is that feasible to have that data generated within a script? If servers.txt is automated by another process, then your main script potentially never needs to be changed as it can read a dynamic file. So either way could be beneficial. Commented Feb 14, 2020 at 18:11

3 Answers 3

4

From the comments, it appears you want to copy and paste a list of systems into a script and have it process as an array. One way to do this is using here-strings.

$servers = @'
server1
server2
server3
'@ -split '\r?\n'

Output

$servers
server1
server2
server3
$servers.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String[]                                 System.Array

Just paste your server list between the @''@ lines. Keep in mind that @' and '@ should be on lines with no other values.

See About Quoting Rules for information on here-strings.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is exactly what I was looking for!
1

Filter out the empty lines:

Get-Content -Path .\servers.txt | Where-Object -FilterScript {$_ -ne ''}

Edit: Sure, you can include your server names within your script if you choose to do that. If you have an array, as you've created in your example, then simply iterate over them within a looping construct, doing something to each server, on each loop.

$Servers = @('Server1','Server2','Server3')

Foreach ($Server in $Servers) {
    "Do something to $Server."
} # End Foreach.

Do something to Server1.
Do something to Server2.
Do something to Server3.

3 Comments

Sorry if I wasn't clear.... I am looking for a way to accomplish the same thing but all in a single script. I want to list the variables in the script as a list instead of importing from an external file.
Still not exactly what I am trying to do... I don't want to have to edit the string, putting it in Single quotes, then adding commas.... Trying to find a way a can paste a list between Parentheses or something.
Yep, use a here-string. See the answer from AdminOfThings.
1

If you want to be able to paste the server names directly into your script without using an intermediate 2nd file, just paste the list into a multi-line string like this:

$serversTxt = @"
server1
server2
server3
"@

$servers = $serversTxt -split "`n"

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.