PS: Template Script
Description:
When writing PS Scripts, I find it helpful to come up with a template type system. Here is mine, feel free to modify for yourself.
To Resolve:
-
These have been moved to my Github
-
My current script will create a folder in the script’s running directory called
PSLogs
and will have a2019-01-01-function-name.log
file associated with it. The good thing about this setup is that since it usesStart-Transcript
, it will record any errors that would normally show on the console. This has been my template for the last two years! - Older scripts like the one that writes to the Event Viewer may be more practical for you, what they do is:
- It creates a file at
c:\scripts\scriptname
automatically for each script. - It writes to the
Application
log in the Windows Event Viewer so it is easy to query. This is great as you will always be able to tell: if your script ran, if it completed, if it had errors, and it can output variables which change each time the script runs. You can easily integrate this in a log aggregation system. - It logs statements like comments.
- It creates a file at
- Before using this template, please make sure to go over Begin, Process, and End in Powershell scripts. For details on how you can use this template to create scripts that run against multiple machines, check out my post on Running Scripts Against Multiple Computers
Comments