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


source to share





All Articles