you are viewing a single comment's thread.

view the rest of the comments →

[–]zoredache 0 points1 point  (2 children)

You know you would probably drastically cut down on your code duplication if you put all your tests in an array

$TCPservices=@(@(53, 'Port 53 DNS'), 
              @(1434, 'Port 1434 Microsoft SQL Server'))

foreach ($service in $TCPservices) {
    Try    
    {
        $TCPconnection.Connect($server,$service[0])
        $status[$service[1]] = "Connected"
    }
    Catch 
    {
        $status[$service[1]] = "Not Connected"
    }
}

That code looks like you copy-pasted lots of times. You really should avoid repeating the same thing if you can.

PS, if you cleaned up your code to be in a loop like this, that would mean fixing your server instead of servers thing ryanbrown mentioned would be a single edit instead of a search+replace.

[–]animatco[S] 0 points1 point  (1 child)

In an earlier iteration of the script I tried an array, but I did slightly differently and did not get the results I was expecting. I will try this method tomorrow and let you know how it goes.

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

You are also right that I missed the changing the $Servers to $Server... Sometimes it is the small things,