Windows Powershell — monitor a process
I learned Powershell over the past few days. It was pretty easy to learn and some of the scripting is very similar to Perl. I still had some issues with my script, I was expecting to parse everything as a text file, but some inputs were objects. I guess that is one of the differences between Windows and UNIX.
My script monitors to see if a particular PID, owned by a particular account is running longer than 10 mins, if yes, write to the event log (or email). Then I created an alert in Sitescope to monitor the event log. One item I noticed was if I typed get-process EXCEL and Excel was not running, then I got a nice pretty error message is red text. I guess Windows doesn’t follow the Silence is Golden mindset.
PS C:\vbs> .\4.ps1
Get-Process : Cannot find a process with the name ‘EXCEL’. Verify the process name and call the cmdlet again.
At C:\vbs\4.ps1:4 char:25
+ $processID = get-process <<<< EXCEL | select id
Here is the UNIX philosophy
1. Small is beautiful.
2. Make each program do one thing well.
3. Build a prototype as soon as possible.
4. Choose portability over efficiency.
5. Store data in flat text files.
6. Use software leverage to your advantage.
7. Use shell scripts to increase leverage and portability.
8. Avoid captive user interfaces.
9. Make every program a filter.10 lesser tenets
1. Allow the user to tailor the environment.
2. Make operating system kernels small and lightweight.
3. Use lowercase and keep it short.
4. Save trees.
5. Silence is golden.
6. Think parallel.
7. The sum of the parts is greater than the whole.
8. Look for the 90-percent solution.
9. Worse is better.
10. Think hierarchically.
Here is the script:
http://www.movement3.com/docs/powershell-monitor-pid
To get Powershell to run from a script:
get-help about_signing
set-executionPolicy remoteSigned
*** Updated 1/2 ***
I had problems with scheduling the Powershell script to run:
C:\Documents and Settings\username>C:\WINDOWS\system32\windowspowershell\v1.0\powe
rshell.exe c:\powershell_scripts\check\script.ps1
Get-Content : Cannot find path ‘C:\Documents and Settings\username\status.txt’ be
cause it does not exist.
At C:\powershell_scripts\RPGSQL_check\script.ps1:6 char:14
+ $status = cat <<<< status.txt
I included the full path: cat C:\powershell_scripts\check\status.txt and it worked.