Create Log File in Powershell

前端 未结 8 2125
半阙折子戏
半阙折子戏 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 22:56

    function WriteLog
    {
        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
    }
    
    WriteLog "This is my log message"
    

提交回复
热议问题