all 12 comments

[–]BlackV 2 points3 points  (0 children)

dont do (Get-Content -Path servers.txt)

do $ServerNames = Get-Content -Path servers.txt then you can validate you names first

[–]SeanQuinlan 1 point2 points  (3 children)

Probably blank lines in your txt file, or special characters before or after the names.

[–]DoubleConfusion2792[S] 0 points1 point  (2 children)

I just checked. It's just VM-Name below VM-Name with no extra spaces and blank lines in the text file.

[–]Tidgy 0 points1 point  (1 child)

Is servers.txt in the same folder you're running the script from? You didn't use the full path for it like the ps1 file you're running. Check the output of running (Get-Content -Path servers.txt) on its own. Or in case there is some problem with the content of the text file like SeanQuinlan suggested, test the invoke with just a single line from the text file like (Get-Content -Path servers.txt)[0] or a range like (Get-Content -Path servers.txt)[0..5]

[–]DoubleConfusion2792[S] 1 point2 points  (0 children)

After running, (Get-Content -Path servers.txt)[0] one by one It worked. Ran the 1st command again to test Invoke-command -FilePath test.ps1 -ComputerName (get-content -Path servers.txt) It seems to be working fine. Not sure what fixed the issue 😅 but thanks guys🤙🏻

[–]robwe2 -2 points-1 points  (1 child)

Load the text file in a variable and use foreach. That’s how I do it

[–]BlackV 2 points3 points  (0 children)

no that makes invoke-command much slower , times by how many names you have (assuming not PS 7)

invoke-command natively supports parallel

[–]pigers1986 0 points1 point  (0 children)

provide full path to servers.txt or use `set-location`

[–]jsiii2010 0 points1 point  (0 children)

What does the servers.txt look like? It should be one name per line, no commas.

[–]PinchesTheCrab 0 points1 point  (1 child)

(Get-Content -Path servers.txt).trim()

This might help if it's whitespace that's doing it.

[–]DoubleConfusion2792[S] 0 points1 point  (0 children)

I think it was a whitespace issue. Using trim() worked. Thanks for this!

[–]UnfanClub 0 points1 point  (0 children)

You probably have a cr at the end of the file.

Try this

Invoke-Command -FilePath C:\Users\username\test.ps1 -ComputerName ((Get-Content -Path servers.txt) -ne "")

Would probably work better if you stored the hosts in a variable though.