- I am trying to create Error logs for my PowerShell script. I need to create a function so that instead of using Write-Host I can directly call that function and whatever the error I am getting in the Script can be directly logged into the Log File.
- I have used the following method, but it doesn't seem that will work for every PowerShell script.
function Write-Log { param ( [string]$LogString ) $LogFile = "C:\$(gc env:computername).log" $DateTime = "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date) $LogMessage = "$Datetime $LogString" Add-content $LogFile -value $LogMessage } Write-Log "This is my log message" Can Anyone Suggest a more easy way to handle logging of Error into the file?