views:

389

answers:

2

This is a duplicate of http://serverfault.com/questions/102098/powershell-script-showing-commands-run. I thought it would be more appropriate to ask this question here.

I am playing around with PowerShell scripts and they're working great. However, I am wondering if there is any way to also show all the commands that were run, just as if you were manually typing them in yourself. This would be similar to "echo on" in batch files. I looked at the PowerShell command-line arguments, the cmdlets, but I didn't find anything obvious.

+1  A: 
C:\workspaces\silverlight> start-transcript -?

NAME
    Start-Transcript

SYNOPSIS
    Creates a record of all or part of a Windows PowerShell session in a text file.


SYNTAX
    Start-Transcript [[-Path] <string>] [-Append] [-Force] [-NoClobber] [-Confirm] [-WhatIf] [<CommonParameters>]


DESCRIPTION
    The Start-Transcript cmdlet creates a record of all or part of a Windows PowerShell session in a text file. The transcript includes all command that the user
     types and all output that appears on the console.


RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113408
    Stop-Transcript 

REMARKS
    To see the examples, type: "get-help Start-Transcript -examples".
    For more information, type: "get-help Start-Transcript -detailed".
    For technical information, type: "get-help Start-Transcript -full".

Note #1: it only records things written to the main console output stream, not Warning / Error / Debug.

Note #2: if you need to record native console applications, you'll need a slight workaround

Richard Berg
I biffed this too - but Start-Transcript does capture the other streams (except for the progress stream). It just doesn't capture EXE output unless you use the Out-Default trick that Andy refers to everywhere you use an EXE.
Keith Hill
+1  A: 

Start-Transcript doesn't catch any exe output. That's a show stopper for me. I hate to say it but the best way I've found to do this is:

cmd /c powershell.exe -file c:\users\hillr\foo.ps1 > foo.log

This captures everything AFAICT.

Keith Hill
Just saw this the other day. http://blogs.msdn.com/powershell/archive/2010/01/04/workaround-for-start-transcript-on-native-processes.aspxNot ideal, but if you pipe exe output to Out-Default, it will get captured
Andy Schneider
The problem is you have to sprinkle this all over your script, wherever you call an EXE. And if you invoke someone else's script that calls an EXE, you have to modify their script. So yeah, not ideal. :-)
Keith Hill
Ah, Andy's link probably explains why I wasn't seeing much with start-transcript. I'll play around with it some more tomorrow, along with Keith's idea (and Richard's below).
Nelson