0
  1. 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.
  2. 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?

1
  • 2
    "but it doesn't seem that will work for every PowerShell script" - do you have an example of a script for which this doesn't work? Commented Sep 22, 2020 at 19:35

2 Answers 2

1

You are not far off for your use case.

You don't need Write-Host at all for this at all. Depending on what PS version you are using, Write-Host is not prudent when you are sending data down the pipe or elsewhere

Write-Host is just bad in legacy, pre-v5x versions if you were sending stuff in the pipeline because it cleared the buffer, so they'd be noting to send. In v5x and higher. As per the founder/creator of Monad/PowerShell.

• Write-Host Harmful

it now writes to the Information stream, as per the founder/creator of Monad/Powershell.

However, this thought has been changed since the v5x stuff.

You can use direct logging using either

Tee-Object Export-Csv -Append Out-File -Append 

... and other redirection options.

You can also, create and write to your own event log...

New-EventLog -LogName LogonScripts -Source ClientScripts Write-EventLog LogonScripts -Source ClientScripts -Message 'Test Message' -EventId 1234 -EntryType Warning 

...then read from it later as you would any other event log.

There are lots of examples (scripts and modules) all of the web to use as is and start with or tweak as needed, even the ones via the Microsoft powershellgallery.com.

Point of note in your post. This...

$LogFile = "C:\$(gc env:computername).log" 

... just simplify to this...

$LogFile = "C:\$($Env:COMPUTERNAME).log" 
Sign up to request clarification or add additional context in comments.

Comments

0
#set logfile-path on global scope $Logfile = "C:\Temp\Logfile.log" # set your Logfile-Path here function Write-Log { param ( [Parameter(ValueFromPipeline)] [string]$content ) $FileExists = Test-Path -Path $LogFile $DateNow = Get-Date -Format 'dd.MM.yyyy HH:mm' $FileInp = $DateNow + ' | ' + $content       if ($FileExists -eq $True){ Add-Content -Path $LogFile -Value $FileInp  } else { New-Item -Path $Logfile -ItemType file Add-Content -Path $LogFile -Value $FileInp } } #then you only have to pipe it to Write-log like this: Write-Output "hello world" | Write-Log 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.