Monitoring with PowerShell: Monitoring listening applications

In one of the online communities I follow someone encountered an issue with application listeners and ports being in use. The use case is that users have a Autocad type application installed that launches a server on a specific port; the users also run a remote control application that at times steals the port.

We’ve seen this all before – IIS being installed on a server and a new application also wanting to use port 443 or port 80, so I figured I’d help by creating him a monitoring script. This script is multifunctional; you can have it alert on changed listeners, you can have it alert on a specific one you expect to be there, or you can alert on applications that should not be listening.

The Script

Change the script to your own wishes, you can either remove the if statements at the bottom, or change the variables at top. 🙂

$ExpectedApplication = "vmms"
$NotAllowedApplication = "Teamviewer"
$CompareToPrevious = $true

$Connections = Get-NetTCPConnection |  Where-Object { $_.LocalAddress -eq "0.0.0.0" -and $_.State -eq "Listen" }
$Processes = Get-Process

$CurrentListeners = foreach ($Connection in $Connections) {
   [PSCustomObject]@{
      Process = ($Processes | Where-Object id -eq $Connection.OwningProcess).Name
      Port    = ($Connection.LocalPort)
Path = ($Processes | Where-Object id -eq $Connection.OwningProcess).Path
}
}

if ($ExpectedApplication -notin $CurrentListeners.Process) {
write-host "Unhealthy - The expected application was not found in the current process list"
}
else {
write-host "Healthy - The expected application was found in the current process list"
}

if ($NotAllowedApplication -in $CurrentListeners.Process) {
write-host "Unhealthy - The non-allowed application was found in the current process list"
}
else {
write-host "Healthy - The non-allowed application was not found in the current process list"
}

if ($CompareToPrevious -eq $true) {
   $PreviousResult = Get-content -Path "C:\ProgramData\ListenerMonitor.txt" | ConvertFrom-Json
   $Result = Compare-Object $CurrentListeners $PreviousResult -Property Process,Port,Path
   if ($Result) {
write-host "Unhealthy - The Listener list has changed."
$Result
}
else {
write-host "Healthy - The Listener list is still the same."
}
$CurrentListeners | ConvertTo-Json |out-file "C:\ProgramData\ListenerMonitor.txt"
}

and that’s it! as always, Happy PowerShelling.

Recent Articles

Automating with PowerShell: making stolen tokens useless with Global Secure Access

Last week I watched Merill Fernando talk to Alex Pavlovsky and Marilee Tuscak from the Entra team about something I’d kinda put under the “needs a GSA licence” branch and i’d check it out later, turns out that like most people, I half-assed the documentation and it turns out it doesn’t. There’s two features that come with any P1 license; Compliant network check and Source IP Restoration. So BP, P1, or even frontline worker licenses already have this.

CIPP ❤️ #IntuneForMSPs

We’re Joining #IntuneForMSPs 🎉

Being invited by Microsoft to join a global initiative is a big moment for us, and we want to be clear about why it matters. #IntuneForMSPs is Microsoft’s program to help MSPs deliver Microsoft 365, Intune, and Copilot services at scale, and CIPP is now part of it, bringing the largest MSP community in the channel directly to Microsoft.

The return of CyberDrain CTF

CyberDrain CTF returns! (and so do I!)

It’s been since september that I actually picked up a digital pen equivalent and wrote anything down. This was due to me being busy with life but also my side projects like CIPP. I’m trying to get back into the game of scripting and blogging about these scripts. There’s still so much to automate and so little time, right? ;)