Start-Transcript和Logging Batch File Output

时间:2017-04-04 09:36:22

标签: powershell batch-file logging module transcription

我在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
    }
}

有没有人对如何捕获批处理文件输出有任何想法?最好不要从脚本中更改对批处理文件的每次调用,并在模块中进行操作。

0 个答案:

没有答案
相关问题