开始转录并记录批处理文件输出

问题描述 投票:0回答:1

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

运行调用批处理文件的 PowerShell 脚本时(我们在从 CMD > PowerShell 迁移时经常这样做),批处理文件输出显示在控制台上与 PowerShell 脚本相同的窗口中,但转录日志文件仅显示 1调用批处理文件的空行。

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

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

您必须制作 PowerShell cmdlet 的输出部分,因为
powershell batch-file logging module transcription
1个回答
0
投票
仅处理 PowerShell。

@Xangrim 建议管道到 
Out-Host

。您还可以尝试将批处理调用包装在 PowerShell 函数中。

© www.soinside.com 2019 - 2024. All rights reserved.