Powershell 不创建日志文件
Powershell not creating log file
我有一个 powershell 脚本,我正在其中更新很多东西,我想将所有内容写入日志文件。我使用了与此类似的方法来编写 CSV 文件,但无法正常工作。谁能帮我解决这个问题?除了没有创建日志文件外,一切正常。
$path = Split-Path -parent "\srv02file\Public\Information Technology\IT Reports\*.*"
$LogDate = Get-Date -f yyyyMMddhhmm
$LogFile = $path + "Remove_Formatting_$LogDate.log"
#Log function with levels of logging and time entry. Default level is INFO when not set
#LogWrite -LEVEL INFO -Message "Log entry"
#HH:MM:SS INFO Log entry
Function LogWrite
{
[CmdletBinding()]
Param (
[Parameter(Mandatory=$False)]
[ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")]
[String]
$Level = "INFO",
[Parameter(Mandatory=$True)]
[string]
$Message
#[Parameter(Mandatory=$False)]
#[string]
#$LogPath
)
$Stamp = (Get-Date).toString("hh:mm:ss")
$Line = "$Stamp $Level $Message"
Add-Content $LogFile -Value $Line
Write-Output $Line
}
$LogFile
中的文件夹后缺少尾部斜线
使用 Join-Path 合并文件夹和文件名以避免将来出现该问题。
您还需要取消注释 $LogPath
参数并使用它,因为 $LogFile
不存在于函数作用域中。
我有一个 powershell 脚本,我正在其中更新很多东西,我想将所有内容写入日志文件。我使用了与此类似的方法来编写 CSV 文件,但无法正常工作。谁能帮我解决这个问题?除了没有创建日志文件外,一切正常。
$path = Split-Path -parent "\srv02file\Public\Information Technology\IT Reports\*.*"
$LogDate = Get-Date -f yyyyMMddhhmm
$LogFile = $path + "Remove_Formatting_$LogDate.log"
#Log function with levels of logging and time entry. Default level is INFO when not set
#LogWrite -LEVEL INFO -Message "Log entry"
#HH:MM:SS INFO Log entry
Function LogWrite
{
[CmdletBinding()]
Param (
[Parameter(Mandatory=$False)]
[ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")]
[String]
$Level = "INFO",
[Parameter(Mandatory=$True)]
[string]
$Message
#[Parameter(Mandatory=$False)]
#[string]
#$LogPath
)
$Stamp = (Get-Date).toString("hh:mm:ss")
$Line = "$Stamp $Level $Message"
Add-Content $LogFile -Value $Line
Write-Output $Line
}
$LogFile
使用 Join-Path 合并文件夹和文件名以避免将来出现该问题。
您还需要取消注释 $LogPath
参数并使用它,因为 $LogFile
不存在于函数作用域中。