Create Log File in Powershell

前端 未结 8 2111
半阙折子戏
半阙折子戏 2020-11-30 22:11

I have the below code and currently it loads all the information on screen. I want it to log to a log file on D:\\Apps\\Logs.

The log file needs to have the name of

相关标签:
8条回答
  • 2020-11-30 23:01

    Gist with log rotation: https://gist.github.com/barsv/85c93b599a763206f47aec150fb41ca0

    Usage:

    . .\logger.ps1
    Write-Log "debug message"
    Write-Log "info message" "INFO"
    
    0 讨论(0)
  • 2020-11-30 23:02

    You might just want to use the new TUN.Logging PowerShell module, this can also send a log mail. Just use the Start-Log and/or Start-MailLog cmdlets to start logging and then just use Write-HostLog, Write-WarningLog, Write-VerboseLog, Write-ErrorLog etc. to write to console and log file/mail. Then call Send-Log and/or Stop-Log at the end and voila, you got your logging. Just install it from the PowerShell Gallery via

    Install-Module -Name TUN.Logging
    

    Or just follow the link: https://www.powershellgallery.com/packages/TUN.Logging

    Documentation of the module can be found here: https://github.com/echalone/TUN/blob/master/PowerShell/Modules/TUN.Logging/TUN.Logging.md

    0 讨论(0)
提交回复
热议问题