I am trying to make PowerShell automatically prepend a timestamp to every line subsequent to invoking my custom ShowTimeStamps function. The function attempts to redirect output thru a filter that prepends the timestamp.
function ShowTimeStamps { $originalOutput = $host.UI.RawUI.WriteLine $host.UI.RawUI.WriteLine = { $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fffffff" Write-Host "$timestamp $_" } } The above function is accepted by Powershell, but cannot be successfully invoked. It produces the error:
The property 'WriteLine' cannot be found on this object. Verify that the property exists and can be set. At line:3 char:5 + $host.UI.RawUI.WriteLine = { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyAssignmentException
prompt. assigning a value toWriteLine(a method) is not valid in this case