VBScript – Random IT Utensils https://blog.adamfurmanek.pl IT, operating systems, maths, and more. Sat, 02 Jan 2021 19:08:39 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 Capturing all output in Powershell https://blog.adamfurmanek.pl/2019/10/05/capturing-all-output-in-powershell/ https://blog.adamfurmanek.pl/2019/10/05/capturing-all-output-in-powershell/#respond Sat, 05 Oct 2019 08:00:56 +0000 https://blog.adamfurmanek.pl/?p=3143 Continue reading Capturing all output in Powershell]]> Today very simple trick to capture whole output from Powershell. Let’s see those files:

1.ps1

echo "1_1 STD OUT"
write-host "1_2 HOST OUT"
write-error "1_3 STD ERR"

.\2.ps1

2.ps1

echo "2_1 STD OUT"
write-host "2_2 HOST OUT"
write-error "2_3 STD ERR"

.\3.bat

3.bat

echo "3_1 STD OUT"
echo "3_2 STD ERR" 1>&2
echo "3_2 STD 3" 1>&3

cscript 4.vbs

4.vbs

Wscript.Echo "4_1 STD OUT"
Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
Set stderr = fso.GetStandardStream (2)
stdout.WriteLine "4_2 STD OUT"
stderr.WriteLine "4_3 STD ERR"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec("5.exe")
WScript.StdOut.Write WshShellExec.StdOut.ReadAll
WScript.StdErr.Write WshShellExec.StdErr.ReadAll

5.exe

#include <iostream>
#include <cstdlib>

int main()
{
    std::cout << "5_1 STD OUT" << "\n";
    std::cerr << "5_2 STD ERR" << "\n";

    system("6.exe");
}

6.exe

#include <iostream>
#include <cstdlib>

int main()
{
    std::cout << "6_1 STD OUT" << "\n";
    std::cerr << "6_2 STD ERR" << "\n";
}

Let’s now run this with powershell:

PS C:\Afish\Playground> .\1.ps1
1_1 STD OUT
1_2 HOST OUT
C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ .\1.ps1
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1

2_1 STD OUT
2_2 HOST OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR
At C:\Afish\Playground\1.ps1:5 char:1
+ .\2.ps1
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1


C:\Afish\Playground>echo "3_1 STD OUT"
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2
"3_2 STD ERR"

C:\Afish\Playground>echo "3_2 STD 3"  1>&3
"3_2 STD 3"

C:\Afish\Playground>cscript 4.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_2 STD OUT
4_3 STD ERR
5_1 STD OUT
6_1 STD OUT
5_2 STD ERR
6_2 STD ERR

Okay, we have all STD OUT and STD ERR streams. Let’s now try redirecting this to file:

PS C:\Afish\Playground> .\1.ps1 > out.txt
1_2 HOST OUT
C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ .\1.ps1 > out.txt
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1

2_2 HOST OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR
At C:\Afish\Playground\1.ps1:5 char:1
+ .\2.ps1
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1

"3_2 STD ERR"
4_3 STD ERR
5_2 STD ERR
6_2 STD ERR

File content:

1_1 STD OUT
2_1 STD OUT

C:\Afish\Playground>echo "3_1 STD OUT" 
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2 

C:\Afish\Playground>echo "3_2 STD 3"  1>&3 
"3_2 STD 3" 

C:\Afish\Playground>cscript 4.vbs 
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_2 STD OUT
5_1 STD OUT
6_1 STD OUT

Okay, so standard output was redirected correctly but standard error and write-host was not. Let’s try this:

PS C:\Afish\Playground> .\1.ps1 2>&1 > out.txt
1_2 HOST OUT
2_2 HOST OUT

File content:

1_1 STD OUT
C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ .\1.ps1 2>&1 > out.txt
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1
 
2_1 STD OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR
At C:\Afish\Playground\1.ps1:5 char:1
+ .\2.ps1
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1
 

C:\Afish\Playground>echo "3_1 STD OUT" 
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2 
.\3.bat : "3_2 STD ERR" 
At C:\Afish\Playground\2.ps1:5 char:1
+ .\3.bat
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: ("3_2 STD ERR" :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 

C:\Afish\Playground>echo "3_2 STD 3"  1>&3 
"3_2 STD 3" 

C:\Afish\Playground>cscript 4.vbs 
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_3 STD ERR
4_2 STD OUT


5_1 STD OUT
6_1 STD OUT
5_2 STD ERR
6_2 STD ERR

Okay, we have almost whole output in the file. Only write-host was not redirected. Can we do better?

The trick is to use CMD:

PS C:\Afish\Playground> cmd /c powershell.exe -file "1.ps1" 4>&1 3>&1 2>&1 > out.txt

File content:

1_1 STD OUT
1_2 HOST OUT
cmd : C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ cmd /c powershell.exe -file "1.ps1" 4>&1 3>&1 2>&1 > out.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (C:\Afish\Playground\1.ps1 : 1_3 STD ERR:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1

 

2_1 STD OUT
2_2 HOST OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR

At C:\Afish\Playground\1.ps1:5 char:1

+ .\2.ps1

+ ~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1

 


C:\Afish\Playground>echo "3_1 STD OUT" 
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2 
"3_2 STD ERR" 


C:\Afish\Playground>echo "3_2 STD 3"  1>&3 
"3_2 STD 3" 

C:\Afish\Playground>cscript 4.vbs 
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_2 STD OUT
4_3 STD ERR


5_1 STD OUT
6_1 STD OUT
5_2 STD ERR
6_2 STD ERR

Now we have everything in file. How to see it in the console as well?

PS C:\Afish\Playground> cmd /c powershell.exe -file "1.ps1" 4>&1 3>&1 2>&1 | Tee-Object -FilePath out.txt
1_1 STD OUT
1_2 HOST OUT
cmd : C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ cmd /c powershell.exe -file "1.ps1" 4>&1 3>&1 2>&1 | Tee-Object -File ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (C:\Afish\Playground\1.ps1 : 1_3 STD ERR:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1

2_1 STD OUT
2_2 HOST OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR
At C:\Afish\Playground\1.ps1:5 char:1
+ .\2.ps1
+ ~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1


C:\Afish\Playground>echo "3_1 STD OUT"
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2
"3_2 STD ERR"

C:\Afish\Playground>echo "3_2 STD 3"  1>&3
"3_2 STD 3"

C:\Afish\Playground>cscript 4.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_3 STD ERR
4_2 STD OUT
5_1 STD OUT
6_1 STD OUT
5_2 STD ERR
6_2 STD ERR

File output:

1_1 STD OUT
1_2 HOST OUT
cmd : C:\Afish\Playground\1.ps1 : 1_3 STD ERR
At line:1 char:1
+ cmd /c powershell.exe -file "1.ps1" 4>&1 3>&1 2>&1 | Tee-Object -File ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (C:\Afish\Playground\1.ps1 : 1_3 STD ERR:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,1.ps1

 

2_1 STD OUT
2_2 HOST OUT
C:\Afish\Playground\2.ps1 : 2_3 STD ERR

At C:\Afish\Playground\1.ps1:5 char:1

+ .\2.ps1

+ ~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,2.ps1

 


C:\Afish\Playground>echo "3_1 STD OUT" 
"3_1 STD OUT"

C:\Afish\Playground>echo "3_2 STD ERR"  1>&2 
"3_2 STD ERR" 


C:\Afish\Playground>echo "3_2 STD 3"  1>&3 
"3_2 STD 3" 

C:\Afish\Playground>cscript 4.vbs 
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

4_1 STD OUT
4_3 STD ERR


4_2 STD OUT
5_1 STD OUT
6_1 STD OUT
5_2 STD ERR
6_2 STD ERR

Now it works correctly.

]]>
https://blog.adamfurmanek.pl/2019/10/05/capturing-all-output-in-powershell/feed/ 0