Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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. Thanks!

share|improve this question

4 Answers 4

help about_History 

Will tell you about all the commands and

Get_History 

will return the full list for you to manipulate\display etc.

share|improve this answer
1  
I found "set-psdebug -trace 1" which will show the commands, but a lot of extra "noise" I don't want. get-history doesn't output anything in a script. If it did I could stick it at the end of a script, but then the commands wouldn't be in-line (before the command output) and any exceptions would skip it (unless I catch it of course). It's still a good one to keep in mind... Any other ideas? –  Nelson Rothermel Jan 12 '10 at 23:55
    
Ah - do you mean that you want to capture the input and the output, or at least you want to echo the input commands immediately followed by corresponding output? One way to do that is to push the results into an array and then loop through the get-history results outputting the command followed by its output. –  Helvick Jan 13 '10 at 0:07
    
start-transcript might also do what you're looking for but it's going to be limited to the console text part of the action only. –  Helvick Jan 13 '10 at 0:15
    
You got it--the same way a batch file works. We have a bunch of batch files now and I'm looking at the feasibility of replacing those with PowerShell scripts. We have software for scheduling, maintaining output history, etc. Without the input command being echoed, it's harder to debug. I'm guessing pushing the results requires you do that on every command, make sure you catch exceptions, etc. so you don't miss anything. Another option, but not quite what I'm looking for. We may just have to pick one option and run with it. –  Nelson Rothermel Jan 13 '10 at 12:47
    
I tried start-transcript, but it only logged that it started/stopped, but nothing in between. I read somewhere it's meant for interactive commands. –  Nelson Rothermel Jan 13 '10 at 12:48

Ugly:

PS > get-content foo.ps1|foreach-object{$_;invoke-expression "$_"}
$procs=get-process powershell
foreach($proc in $procs){$proc.processname}
powershell
PS > get-content foo.ps1
$procs=get-process powershell
foreach($proc in $procs){$proc.processname}
PS >

The problem with the above is that if you have multi-line commands like this:

foreach($proc in $procs){
  $proc.processname
}

The above will fail with my example above if that's placed in foo.ps1 with that structure...

share|improve this answer
    
Yeah, I keep finding half-solutions like this. I really think there isn't a "silver bullet" for what I want. Thanks for being creative with these solutions. I have a couple of options now and will have to decide where to go from here. –  Nelson Rothermel Jan 21 '10 at 2:26

Where I used echo on in CMD, I now use Write-Verbose and Set-PSDebug -Step instead. They are not the same, but they are more powerful if wielded skillfully.

share|improve this answer

The following command will output each line of script to Write-Debug-

Set-PSDebug -Trace 1

From man Set-PSDebug

When the Trace parameter is set to 1, each line of script is traced as it is executed. When the parameter is set to 2, variable assignments, function calls, and script calls are also traced. If the Step parameter is specified, you are prompted before each line of the script is executed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.