Let’s do something useful
When I am writing PowerShell scripts for customers, I like to make sure that the script records information about what it is doing and any changes it has made in a log file so that I can quickly pinpoint what is going on. I don’t write the functionality each time I write a script, however; I just include some saved PowerShell snippets in the script that give me a log file function. Let’s think about how we might do that now.
First, we need to think about where we create the log file. I use C:\temp
, as that directory is usually where we put things that we don’t want to keep forever, and it usually has fairly relaxed permissions. We might want to think about a different directory on Linux or macOS, such as /var/log/
.
We also want to create a file that is as easy to read as possible; I write to a text file but I use the suffix .log
so that I know what sort of text file it is. The file we create needs to be date-stamped so that...