I am trying to export the powershell script logging and the log file will contains the name of the computer where the script will be running. I have tried this using start-Transcript but it omits the name of the computer in the file.
Maybe there is a better way to do it any recommendation and advice will be greatly appreciated.
# Start logging
$Computer = $env:computername
Start-Transcript -path "C:\Temp\$Computer_$((Get-Date).ToString('MMddyyyyhhmm')).txt" -NoClobber -IncludeInvocationHeader
$file = "C:\Temp\032520221151.txt"
# If file does not exist, exit
if (!(Test-Path $file))
{
Write-Output "File doesn't exist!"
}
# Test if output file exist, if so delete it
else
{
Remove-Item $file
}
And it looks like the Start-Transcript does not log the error. When I tested it with error, it was not showing up the log file. I would like to be able to see the error too.
This way you have CSV file you could even re-import later on with PowerShell and create a report with it.
Errors will be written to another stream. I think it would be more professional anyway to handle errors with a proper error handling with a try catch block.
I like the way you format your script. You’re right the Start-Transcript lot of unneedy info.
I have noticed that you exported the results or output to a CSV file. That is interesting if it can be appended when run on multiple devices. I will steal that.
But there is hardly anything to log in your script? Anyway, either use start-transcript or just output a status message before and after each step you want to log and write it to a log file.
But you should keep in mind that writing to a log file repeatedly will slow down your script considerably.