Start-Transcript and Batch File Writing

I have a function in a PowerShell module that creates a log file and runs a transcript using that file (see below). When working with a PowerShell script, this works great and captures all output.

When running a PowerShell script that calls a batch file (which we do quite often when migrating from CMD> PowerShell), the output of the batch file is displayed on the console in the same window as the PowerShell script, but the log file only shows 1 blank line where call in batch file.

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

      

I am calling batch files from .ps1 scripts with ampersand '&' only.

What's strange is that sometimes the output of a batch file file is written to the log (usually the first batch file). However, I can't find anything special in these files.

It is also strange that sometimes we call external programs (WinSCP) and the output from these commands is only sometimes shown in the decryption. Perhaps relevant.

For reference, here is the function that I use to create a transcript of our processes.

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

      

Does anyone have any ideas on how I can capture the output of a batch file? Preferably, I wouldn't have to change every batch file call from the script and do things in the module.

+3
module logging powershell batch-file


source to share


No one has answered this question yet

See similar questions:

fourteen
start-Transcript does not write all output to the log file ..?

or similar:

3061
grep file but show multiple surrounding lines?
1084
How can I pass arguments to a batch file?
908
How to have git log show filenames like svn log -v
706
Windows batch files: .bat vs .cmd?
618
How to sleep for five seconds in a batch file / cmd
605
Splitting long commands over multiple lines via batch file
2
PowerShell 1 does not capture tee package output file
2
Powershell starts transcription
0
Powershell - Problem with Start Transcription using Remote
0
Windows PowerShell calls a function with parameters



All Articles
Loading...
X
Show
Funny
Dev
Pics