Powershell 启动成绩单并记录批处理文件输出

Powershell 启动成绩单并记录批处理文件输出,powershell,batch-file,logging,module,transcription,Powershell,Batch File,Logging,Module,Transcription,我在PowerShell模块中有一个函数,它创建一个日志文件并使用该文件启动一个成绩单(见下文)。在运行PowerShell脚本时,这非常有效,可以捕获所有输出 当运行调用批处理文件的PowerShell脚本时(我们在从CMD>PowerShell迁移时经常这样做),批处理文件输出将显示在控制台上与PowerShell脚本相同的窗口中,但成绩单日志文件仅显示一个空白行,其中显示了对批处理文件的调用 09:53:25 AM [Success] Zip file already up to date

我在PowerShell模块中有一个函数,它创建一个日志文件并使用该文件启动一个成绩单(见下文)。在运行PowerShell脚本时,这非常有效,可以捕获所有输出

当运行调用批处理文件的PowerShell脚本时(我们在从CMD>PowerShell迁移时经常这样做),批处理文件输出将显示在控制台上与PowerShell脚本相同的窗口中,但成绩单日志文件仅显示一个空白行,其中显示了对批处理文件的调用

09:53:25 AM [Success] Zip file already up to date, no need to download!
09:53:25 AM [Note   ] Calling 1.bat

10:07:55 AM [Note   ] Calling 2.bat
我正在从.ps1脚本调用批处理文件,只使用了符号“&”

奇怪的是,有时批处理文件输出被捕获到日志中(通常是第一个批处理文件)。但是我找不到这些文件有什么特别之处

同样奇怪的是,有时我们调用外部程序(WinSCP),这些命令的输出有时只显示在转录本中。可能相关

作为参考,这里是我用来创建我们流程的转录本的函数

Function Log_Begin()
{
    <#
    .SYNOPSIS
    Starts the process for logging a PowerShell script.
    .DESCRIPTION
    Starts the process for logging a PowerShell script. This means that whenever
    this function is called from a PowerShell script, a folder called 'Logs' will
    be created in the same folder, containing a full transcript of the script's output.
    .EXAMPLE
    C:\PS> Log_Begin
    #>
    Process
    {
        $ScriptLoc = $MyInvocation.PSCommandPath
        $WorkDir = Split-Path $ScriptLoc
        If (!(Test-Path "$WorkDir\Logs")) {mkdir "$WorkDir\Logs" | Out-Null}
        $LogPath = "$WorkDir\Logs"
        $ScriptName = [io.path]::GetFileNameWithoutExtension($ScriptLoc)
        $LogDate = Get-Date -format "yyyy-MM-dd"
        $LogName = "$ScriptName $LogDate.log"
        $global:Log = $LogPath + "\" + $LogName
        $ErrorActionPreference="SilentlyContinue"
        Stop-Transcript | out-null
        $ErrorActionPreference = "Continue"
        # Create file and start logging
        If (!(Test-Path $Log)) {
            New-Item -Path $Log -ItemType File | Out-Null
        }
        Start-Transcript -Path $Log -Append
    }
}
函数日志\u Begin()
{

看起来像一个bug,请看这里@BaliC似乎常见的解决方法是在通话结束后附加“| Out Host”。我已经添加了它,并将尝试测试。谢谢!酷,希望它能工作!:)@BaliC嗨,工作得很好!再次感谢。好的,那么你们中的一位应该把它作为这个问题的答案发布,这样我们就可以解决它了。