Am I getting too greedy with test-netconnection -computername .... cmdlet?

Matt Fortune 26 Reputation points
2020-11-16T17:26:40.477+00:00

PowerShell novice here wants to know If/How I can execute this cmdlet with Servers.txt being a simple list 8 servers in a list?

Test-NetConnection -ComputerName Get-Content (C:\Temp\SCOM\Servers.txt) -port 5723 -Informationlevel Detailed | Out-File C:\Temp\SCOM\Test-NetConnection_port5723.txt

I get this error: Test-NetConnection : A positional parameter cannot be found that accepts argument '$null'.

What am I getting wrong if there is a better way to run Test-NetConnection query against multiple servers at the same time please advise.

Thanks,

Matt

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,504 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. SChalakov 10,376 Reputation points MVP
    2020-11-16T17:43:36.143+00:00

    Hi Matt,

    what if you try the otehr way arround, like suggested here:

    One-liner : Test-Connection for multiple computers in Powershell
    https://gallery.technet.microsoft.com/scriptcenter/One-liner-Test-Connection-373858ef

    get-content servers.txt | % {new-object psobject -property @{ComputerName=$_; Reachable=(test-connection -computername $_ -quiet -count 1)} }  
    

    Here another one, a bit more sophisticated (more checks):

    PowerShell Tip of the Week: Check connectivity to multiple servers
    check-connectivity-multiple-servers-specific-port-using-powershell

    Does this help?

    Regards,
    Stoyan

    2 people found this answer helpful.

  2. Rich Matheisen 46,551 Reputation points
    2020-11-16T19:44:36.32+00:00

    StoyanChalakov already helped you to get the result you wanted, but nobody's explained why what you originally tried failed to work.

    1. Read the help for the Test-Connection cmdlet. The -ComputerName parameter accepts a single value, not an array of values (which is what Get-Content would have returned.
    2. By placing only "C:\temp\SCOM\servers/txt" within parentheses you told PowerShell to evaluate the expression. On your system, it opened NotePad.exe and displayed the contents of the file! That's because the .txt file extension is associated with the NotePad executable.
    3. If, on some other cmdlet that does accept an array of values, you wanted to use the contents of a file, place the parentheses around the entire Get-Content (e.g. "(Get-Content c:\temp\SCOM\servers.txt)"
    1 person found this answer helpful.
    0 comments No comments

  3. Matt Fortune 26 Reputation points
    2020-11-16T22:21:00.917+00:00

    Thank You Stoyan!

    Doing it the other way around putting Get-Content first then | cmdlet worked.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.