I have been working on a project off and on just to learn Powershell. One issue I am running into is setting up a script that can scan ports from machines. The script I have uses txt files for for the ports and servers.
What I would like my script to do is scan a machine from the txt file and determine if the port is open, then go to the next machine and scan it based on the next line to see if that port is open. Kind of like a loop. Right now the script will only work
with a single line from each txt file but if I add more it craps out.
Below is a copy of my original script.
#######################################################################
#
# Powershell NetMon Port Monitor Script
#
# Script scans port to check status if available
#
#######################################################################
#######################################################################
# HTML Variables
#######################################################################
$dated = "<CENTER>"
$dated = $dated + (Get-Date -format F)
$a = "<META HTTP-EQUIV=refresh CONTENT=15>"
$a = $a + "<style>"
$a = $a + "BODY{background-color:#646D7E;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 6px;border-style: solid;border-color: black;}"
$a = $a + "</style>"
#######################################################################
# Get content from config files
#######################################################################
$hostid = (get-content C:\Scripts\cfg\porthosts.cfg | Where {-not ($_.StartsWith('#'))})
$portnumber = (get-content C:\Scripts\portcfg.cfg | Where {-not ($_.StartsWith('#'))})
#######################################################################
# Check status of ports
#######################################################################
$socket = new-object Net.Sockets.TcpClient
$socketresults = $socket.Connect($hostid, $portnumber)
if ($socket.Connected) {
$status = “Port Open”
$socket.Close()
}
else {
$status = “Port Closed”
}
$b = @"
<CENTER>
$dated<BR />
<TABLE>
<TR bgcolor=#2554C7>
<TD>Hosts</TD> <TD>Port Number</TD> <TD>Status</TD>
</TR>
<TR bgcolor=#5CB3FF>
<TD>$hostid</TD> <TD>$portnumber</TD> <TD>$status</TD>
</TR>
</TABLE>
</CENTER>
"@
#######################################################################
#Export to HTML
#######################################################################
$statusupdate | ConvertTo-HTML -head $a -body $b | out-file C:\Scripts\gen\porthosts.htm
I think this is probably the last major part of my network monitoring scripts that needs tweaking. Yes I know there are hundreds of thousands of network monitoring tools but this is pretty useful for me to learn Powershell and its syntax. Once this is complete I will make it public so that other I.T. professionals can play with it.
In advance thank you for your help!