在 Foreach 循环中触发日志写入?

问题描述

所以我有下面的循环,最后一项:日志写入只是没有开始。

Foreach ($Manager in $Managers) {
    $time = (Get-Date).ToString('T')
    New-bhReportToGroup
    Get-bhDReports
    Set-bhRTGmembers
    Log-Write -LogPath $sLogFile -LineValue "========================[ $time ]==============================="
}

我没有把所有代码都塞满,因为我打赌这并不重要。但是我在所有工作的各种功能中有多个 Log-Write 调用。目标是在日志文件中的每个循环迭代之间引入一个分隔符。

这很好,$Global:DebugPreference 设置为继续,但日志文件中没有任何内容

谢谢!

编辑

日志写入函数执行此操作:

Function Log-Write {
[CmdletBinding(SupportsShouldProcess)]  

  

  Param ([Parameter(Mandatory=$true)][string]$LogPath,[Parameter(Mandatory=$true)][string]$LineValue)

  

  Process{

    Add-Content -Path $LogPath -Value $LineValue

  

    #Write to screen for debug mode

    Write-Debug $LineValue

  }

}

解决方法

将输出重定向到日志的最常用方法是重定向。您可以阅读重定向 here

如果这对您不起作用,您可以使用 Out-File cmdlet。您可以阅读帮助文件 here。使用 Out-file 的最简单方法是在管道中。

"This gets sent to a logfile" | Out-File myfile.log