Sunday, November 10, 2013

Scan your network for open port and take action against it using powershell

Good day
For some reason you may need to run an IP Scanner on your network, most of IP scanner can also perform a port scan, but I did not find free one that can make action against the open port, like test SSL version on the running web server, or get information about the running web service or any other action.
So I decide to write my own script and share it with you.

cls
#for me I just want to get a list of computers that are running Windows 2008.
#You can cancel or modify the filter
#All the result will be loaded to a variable named as allservers
$allservers=Get-ADComputer -Properties name,operatingSystem -Filter{operatingSystem -like "*2008*"}
foreach($singleServer in $allservers){
$socket=New-Object net.Sockets.TcpClient

#its possible to use IF insted of TRY, but you will get an error when the connection fail, so its recommend to use Try/Catch/Finally
#Try Function will be used to initial the connection
#Good information about Try/Catch/Finally Here

    Try{
    #The created Socket will be used to connect to the server on port 443
        $socket.Connect($singleServer.Name,443)
        Write-Host "The port on " $singleServer.Name " is " -NoNewline
        Write-Host "open" -ForegroundColor Green
        C:\SSLScan.exe $singleServer.name
                
            }
        #Use Catch to get the System error
        Catch [system.exception]{
        Write-Host "The port on " $singleServer.Name " is " -NoNewline
        Write-Host "Closed" -ForegroundColor red
        
        }
        
}



I hope you like this script, Please like the page and share it :)
Thanks for reading

No comments: