$LogFile = "C:\temp\TestingThePowerShellService.txt" To stop it, simply stop the service (Stop-Service -Name YourServiceName).įirst I change this line in the sample code script, so I get a new log I can check to see if anything happens and gets logged. It'll run under the SYSTEM account by default. ![]() Installing it as a serviceTo run it, you could do it manually or set it up as a scheduled task that triggers on server startup (only trigger/launch it once, it runs indefinitely), but I will also demonstrate how to set it up as a service with nssm.exe (non-sucking service manager). VERBOSE: Average free memory for 10 samples was 8,474.98 MB. VERBOSE: Average CPU usage percent for 10 samples was 5.61 %. Running command and emptying counter array. Running command and emptying counter array.Įs was 8.2763 GB (trigger value: 10000). VERBOSE: Average CPU usage percent for 10 samples was 6.70 %. VERBOSE: Memory usage alert triggered! Memory usage for the samplĮs was 8.2791 GB (trigger value: 10000). VERBOSE: CPU usage alert triggered! CPU usage for the samples wasĦ.69877037857689 % (trigger value: 3). It should work with PowerShell version 2 and up (default on Server 2008 R2 / Windows 7). This is meant to be adapted to suit your needs. Or you could Splunk / log (beyond the built-in logging) / whatever. ![]() You can add other Send-MailMessage statements to send errors on the various conditions, such as when the condition/code/script is triggered. To run PowerShell code, just replace it, and you probably want a try/catch statement to catch errors and warn you about them, like I do myself in the sample code, with Send-MailMessage. In my sample code I just use "cmd /c echo 1" and check the exit code. The sample code below will allow you to perform an action, such as run "iisreset", run any cmd/batch script, executable file, any PowerShell code, and really do anything a computer running the script/service is capable of doing. I spent some hours writing this and later decided to write a more generic version to share with the world. While working on isolating the root cause, they wanted a script to automatically run iisreset when CPU usage reached 70 % or higher based on a set of sample intervals over the last five minutes. ![]() Possibly some "permanent-use scenarios" as well.Ī client approached me with a need for a temporary workaround for an issue they had where CPU usage would suddenly spike - and the workaround was (possibly at 3 AM for someone on call) to run "iisreset", as dirty as that may seem. This is an example of real world problem solving - or rather problem patching - or working around problems (hopefully temporarily in many cases) - or possibly a bunch of scenarios I haven't considered. Customization with pre-defined variables.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |