diff --git a/content/scripts/ForceClose.ps1 b/content/scripts/ForceClose.ps1 new file mode 100644 index 0000000..5d83552 --- /dev/null +++ b/content/scripts/ForceClose.ps1 @@ -0,0 +1,32 @@ +param ( + [Int32]$targetPort = "8080", + [string]$processName = "java" +) + +function Pluralize($value) +{ + if ($value -eq 1) + { + return "" + } + + return "s" +} + +$connections = Get-NetTCPConnection -state Listen -LocalPort $targetPort -ErrorAction 'SilentlyContinue' + +if ($connections.Length -eq 0) +{ + Write-Host "No ports were open and listening on ${targetPort}" + return +} else +{ + $processes = $connections | % { Get-Process -Id $_.OwningProcess } | ? { $_.Name -like $processName } + $processCount = $processes.Length + Write-Host "Closing ${processCount} process${processCount | Pluralize} open on ${targetPort}..." + + $processes | + Stop-Process -Force -PassThru | + select -Property Name, ProcessName, StartTime, HasExited, ExitTime | + Format-List +} \ No newline at end of file diff --git a/content/scripts/force-close.md b/content/scripts/force-close.md new file mode 100644 index 0000000..0add371 --- /dev/null +++ b/content/scripts/force-close.md @@ -0,0 +1,19 @@ ++++ +title = "Dangling Process Closer" +summary = "A simple script for Firebase Emulators to close dangling processes." +date = 2022-04-28 ++++ + +This script was developed while building a Firebase app that had a local emulator run. Often, this emulator +would freeze up and not close properly, so opening up a Task Manager (I used _Process Hacker_ because it's easier) and +manually close the dangling `java.exe` process. + +This became so common and disruptive that I created a little script to find and kill the process for me. + +It's not that big of a deal, but I gotta add content to this site, so why not... + +Raw file available at {{% absolute_url "./scripts/ForceClose.ps1" %}} + +```powershell +{{% file "/scripts/ForceClose.ps1" %}} +``` \ No newline at end of file diff --git a/content/scripts/verb-count.md b/content/scripts/verb-count.md index fe83f4a..3310af3 100644 --- a/content/scripts/verb-count.md +++ b/content/scripts/verb-count.md @@ -1,6 +1,7 @@ --- title: "Verb Counts" summary: A simple script to count up all the verbs used by commands in PowerShell. +date: 2022-04-19 --- This script uses the `Get-Verb` command to sum and provide the percentage of each verb in the Powershell environment.