Advertisement
1RedOne

SCCM compliant PowerShell log funcion

Jul 21st, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. function Write-Log {
  2. param(
  3. [int]$ErrorLevel=1, # 1 - info, 2 - warning, 3 - error
  4. [Parameter(position=1,ValueFromPipeline=$true)][string]$Msg,
  5. [Parameter(position=2)][string]$Component, # source of the entry
  6. [Parameter(position=3)][string]$LogFile = "c:\temp\LPUlog.log",
  7. [switch]$break,
  8. [switch]$tee
  9.  
  10. )
  11.  
  12. if( !$Component ){ $Component = $PSCommandPath -replace '^.*\\|\.[^\.]*$' } # script name
  13. if( !$LogFile ){ $LogFile = $PSCommandPath -replace '\.ps1$','.log' } # <ScriptRoot>\<ScriptName>.log
  14. if($break){$Msg='#############################################################'}
  15. if($tee){Write-Output $msg}
  16. $TZBias = (Get-WmiObject Win32_TimeZone).bias
  17. $Time = "$(Get-Date -Format 'HH:mm:ss.fff')$TZBias"
  18. $Date = Get-Date -Format 'MM-dd-yyyy'
  19. $Context = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
  20.  
  21. $LogEntry = "<![LOG[$Msg]LOG]!><time=`"$Time`" date=`"$Date`" component=`"$Component`" context=`"$Context`" type=`"$ErrorLevel`" thread=`"$pid`" file=`"`">"
  22.  
  23. Add-Content -Path $LogFile -Value $LogEntry
  24.  
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement