I am running a PS script to stop and restart Services using Restart-Service. For troubleshooting issues I’d like to write the output of every activity to a log file so we can make sure the services stopped and restart.
Transcripts are useful but you end up getting much more info than what might be necessary.
As for the script, you may want to also look at building in some form of error handling as well. This script as it stands right now may produce errors that are not logged. I would suggest reading up on how to use ‘Try / Catch / Finally’ and make this a function that you can feed parameters to, such as service or computer name, and even the path to the log file you want to keep.
Well, if you don’t want to use start-transcript, you can use Write-Log
This is a function that I wrote years back and is now part of the AZSBTools PS module (install-module AZSBTools)
It writes output to the console/and optionally to a log file - it also writes to the console in color lol
Example:
The -passthru option returns an ServiceController object for those commands. You don’t need to run stop-service first.
PS C:\Users\admin> stop-service apple* -passthru
Status Name DisplayName
------ ---- -----------
Stopped Apple Mobile De... Apple Mobile Device Service
PS C:\Users\admin> restart-service apple* -passthru
Status Name DisplayName
------ ---- -----------
Running Apple Mobile De... Apple Mobile Device Service