I use PowerShell to check if any ports are open on my computers. I have eight Windows Server 2008 R2 machines and run the following script:
$localhost = get-content env:computername
foreach($port in get-content "\\computer1\txtfiles\ports.txt")
{
foreach ($hostname in get-content "\\compiuter1\txtfiles\servers.txt")
{
try
{
$sock = new-object System.Net.Sockets.Socket -ArgumentList $([System.Net.Sockets.AddressFamily]::InterNetwork),$([System.Net.Sockets.SocketType]::Stream),$([System.Net.Sockets.ProtocolType]::Tcp)
$sock.Connect($hostname,$Port)
$output = $localhost+","+$hostname+","+$port+","+$sock.Connected
$output
$sock.Close()
}
catch {
$output = $localhost+","+$hostname+","+$port+","+$sock.Connected
$output
}
}
}
I run this script on the eight computers from computer1 using:
Invoke-Command -ComputerName computer1,computer2 -FilePath F:\scripts\port-test.ps1
On the first computer (computer1- the machine that I execute the script from) I get an output but on computer2 I get:
Cannot find path '\\computer1\txtfiles' because it does not exist.
+ CategoryInfo : ObjectNotFound: (\\computer1\txt
files:String) [Set-Location], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLo
cationCommand
Why doesn't PowerShell see the network share? How can I fix it?
foreach $PcName in @ComputerList { Invoke-Command -ComputerName $PcName ... }
. Okay, that's just pseudo-code, but the idea is to only pass one computer name at a time to the command.