So I have a few scripts that run using the task scheduler, and recently I have noticed a shed load of cmd.exe and powershell.exe running, where the script for some reason has not completed. So a bit of googling and found some interesting stuff.
To get the current PID for the powershell session you are running:
[System.Diagnostics.Process]::GetCurrentProcess()
Now the funky bit, get a list of all cmd.exe and powershell.exe that are running:
get-wmiobject win32_process|where{($_.name -eq “powershell.exe”) -OR ($_.name -eq “cmd.exe”)} | sort processid | select processid,processname,commandline | ft –wrap
Now the wicked bit here is the commandline value (wow you can see it in taskmanager too ) it shows you what the commandline is that was used to execute the cmd or powershell.
Now just need to work on a wrapper that will stop the process if it’s already running