Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, September 25, 2012 9:10 AM
Hi there,
I'd like to know how it would be possible, that I could print the whole PowerShell output into a single logifle, as long as one simple script is running. The following happens:
I run one simple script that will run plenty other PowerShell scripts, I'd like to print everything written in the output window of PowerShell, into a logfile.
Lua Programmer & Powershell Newcomer | Location: Switzerland Beside that, whenever you see a reply you think its helpful, mark it as helpful! And whenever you see a reply being an answer to the main question of the thread, mark it as answer (if you opened the thread).
All replies (15)
Tuesday, September 25, 2012 9:54 AM ✅Answered | 2 votes
Or you can use Start-Transcript / Stop-Transcript.
Grant Ward, a.k.a. Bigteddy
Tuesday, September 25, 2012 1:48 PM ✅Answered | 2 votes
Hi Livio !
Fort the easy way you can go with the Transcript approach!
Even you can write a Write-Log Function like Blindrood suggested.
So if you want to manage Logging in PowerShell, you have to deal with the different input and output streams PowerShell is offering:
The concept of streams in PowerShell
All legacy commandline shells like CMD, and Unix shells like Bash, Ksh etc. deal with three streams: stdin, stdout and stderr for Input, Output and Error respectively.
Wikipedia standard streams: http://en.wikipedia.org/wiki/Standard_streams
PowerShell has many more: Input, Output, Verbose, Warning, Debug, Progress and Error.
The main purpose of this is to separate different kind of messages from being included within legitimate output.
The Input stream (stdin) gets the Input (User Input) from the console Window with the Cmdlet Read-Host
The verbose stream displays only messages written with the Write-Verbose Cmdlet, if a script or a Cmdlet is executed with the –Verbose switch. See Preference Variable $VerbosePreference
The warning steam uses the Write-Warning Cmdlet to display nonterminating errors. The response to the warning depends on the value of the user's $WarningPreference variable and the use of the WarningAction common parameter.
The error stream (stderr) represents all (terminating) errors. You can write to the error stream with the Cmdlet Write-Error. See Preference Variables $ErrorActionPreference, $ErrorView
The debug stream is only displayed in the debug mode of the script environment you can user Write-Debug to write to this stream. By default, debug messages are not displayed in the console, but you can display them by using the Debug parameter or the $DebugPreference variable.
You can write to the progress stream with the Write-Progress Cmdlet. See Preference Variable $ProgressPreference
The output stream (stdout) represents the "normal" output of a command with the Cmdlet Write-Host or better : Out-Default
There are different Preference Variables who change the behavior of the Cmdlets:
$ErrorActionPreference, $ErrorView, $WarningPreference, $DebugPreference, $ProgressPreference, $VerbosePreference
For this topic see in the PowerShell Help!
For PowerShell 3.0 it is possible you have to use the –online Parameter with the Get-Help cmdlet to get rich Help!
Get-Help about_Preference_Variables –Full –Online
By default, Windows PowerShell sends its command output to the Windows PowerShell console with Out-Host.
Cmdlet Write-* direct its output to > Stream direct its output to > Out-Default
However, you can direct the output of a stream (not a cmdlet !) to a text file, and you can redirect error stream output to the regular output stream.
In PowerShell 2.0 this was limited to the error and the output stream.
In PowerShell 3.0 you can now redirect all streams except the progress stream!
For this topic see in the PowerShell Help!
For PowerShell 3.0 it is possible you have to use the –online Parameter with the Get-Help cmdlet to get rich Help!
Get-Help about_Redirection –Full –Online
Wikipedia: Redirection http://en.wikipedia.org/wiki/Redirection_%28computing%29
See also:
For PowerShell 3.0 it is possible you have to use the –online Parameter with the Get-Help cmdlet to get rich Help!
Get-Help about_Preference_Variables –Full –Online
Get-Help Write-Error –Full -Online
Get-Help Write-Host –Full -Online
Get-Help Write-Output –Full -Online
Get-Help Write-Progress –Full -Online
Get-Help Write-Verbose –Full -Online
Get-Help Write-Warning –Full –Online
Get-Help Out-Host –Full –Online
Get-Help Out-Default –Full –Online
Get-Help Out-File –Full –Online
Get-Help Out-String –Full –Online
Get-Help Out-Null –Full –Online
Capture Warning, Verbose, Debug and Host Output via alternate streams
http://connect.microsoft.com/PowerShell/feedback/details/297055/capture-warning-verbose-debug-and-host-output-via-alternate-streams
Good Logging Links:
PowerShell Automatic Logging
http://jdhitsolutions.com/blog/2011/03/powershell-automatic-logging/
Please click “Mark as Answer” if my post answers your question and click Vote as Help if my Post helps you.
Bitte markiere hilfreiche Beiträge von mir als Hilfreich und Beiträge die deine Frage ganz oder teilweise beantwortet haben als Antwort.
My PowerShell Blog http://www.admin-source.info
[string](0..21|%{[char][int]([int]("{0:d}" -f 0x28)+('755964655967-86965747271757624-8796158066061').substring(($_*2),2))})-replace' '
Tuesday, September 25, 2012 9:21 AM
.\myhugescript.ps1 | Out-File -Path D:\log.log -Append
$(.\myhugescript.ps1) | Out-File -Path D:\log.log
Tuesday, September 25, 2012 10:28 AM
Blindrood I guess this is for putting into the PowerShell.exe, not script, right? Actually I want to define the output file inside "myhugescript".
Lua Programmer & Powershell Newcomer | Location: Switzerland - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Beside that, whenever you see a reply you think its helpful, mark it as helpful! And whenever you see a reply being an answer to the main
question of the thread, mark it as answer (if you opened the thread).
Tuesday, September 25, 2012 10:40 AM
You can use this construction inside a script. Note that this way You'll be sure you're redirecting all output to a file. When You want to do it inside a script You have to redirect every line
Another approach would be to create function Write-Log which will write everything You want to a file
I use this function in my scripts
$script:loggingpathpreference = 'D:\logs\log.log'Set-PSBreakpoint -Variable Now -Mode Read -Action {Set-Variable Now (get-date -uformat '%Y\%m\%d %H:%M:%S') -Option ReadOnly, AllScope -Scope Global -Force -ErrorAction SilentlyContinue} -ErrorAction SilentlyContinueFunction Write-Log {
[cmdletbinding()]
Param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[ValidateNotNull()]
$Message,
[switch]$Warning
)
Process{
if($Message -isnot [string]){
$Message = $Message | Out-String
}
Write-Verbose $message
Add-Content -Path $script:LoggingPathPreference -Value "`n$($Now) : $($Message)"
if($Warning){
Write-Warning $Message
}
}
}
Then you can pipe everything to this function
Tuesday, September 25, 2012 10:43 AM
And how would I print the error messages to the log file? Your function really confuses me, my lvl on powershell is not that high yet.
Lua Programmer & Powershell Newcomer | Location: Switzerland - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Beside that, whenever you see a reply you think its helpful, mark it as helpful! And whenever you see a reply being an answer to the mainquestion of the thread, mark it as answer (if you opened the thread).
Tuesday, September 25, 2012 10:52 AM
Every string and object that can be casted to string just pipe to this function:
"message" | Write-Log
If you're catching errors then you can do something like that:
try{
Throw "Big error"
}catch{
$_ | Write-Log
}
What I like to do is to write all errors at the end of script to log (just in case I missed something).
Every error is stored in a $Error variable so
$Error | Write-Log
Tuesday, September 25, 2012 11:00 AM
But if every error gets stored in the $Error variable, then it would always print all errors until I use $Error, right? If so, than that would be quite inefficient, since I'm always saying, when an installation started and finished. Inbetween those two messages should be the error messages, but when I always use $Error | Write-Log, it would also print all other error messages, or won't it? Example:
Installing 1...
Showing errors of 1.
1 Installed.
Installing 2...
Showing errors of 1 and 2.
2 Installed.
etc.
Lua Programmer & Powershell Newcomer | Location: Switzerland - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Beside that, whenever you see a reply you think its helpful, mark it as helpful! And whenever you see a reply being an answer to the mainquestion of the thread, mark it as answer (if you opened the thread).
Tuesday, September 25, 2012 11:10 AM
I seem to have problem understanding what You're trying to say. Of course You can only write $error. I do so.
I use Write-Log not only for error but for other messages, output and for basic logging.
I don't need to write-log every error during script because I use $erroractionpreference = 'stop' so any error will stop script and then I use $error variable to write all errors
$ErrorActionPreference = 'stop'
try{
do everything
}
catch {
$Error | Write-Log
}
If You're not catching errors or 'silentlycontinue' them they'll always appear
If You want to collect errors only You can use construct form my first post (just remember to cast it to string) at the end of script
$Error | Out-String | Out-File D:\log.log -Append
Tuesday, September 25, 2012 11:43 AM
Or you can use Start-Transcript / Stop-Transcript.
Grant Ward, a.k.a. Bigteddy
This is the answer I would go for.
Regards / Freundliche Grüsse King Julien
Tuesday, September 25, 2012 11:52 AM
Ok, I'll check that.
Lua Programmer & Powershell Newcomer | Location: Switzerland - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Beside that, whenever you see a reply you think its helpful, mark it as helpful! And whenever you see a reply being an answer to the mainquestion of the thread, mark it as answer (if you opened the thread).
Wednesday, September 26, 2012 6:47 AM
I'll take it the simple way. :)
But thanks for all that information!
Lua Programmer & Powershell Newcomer | Location: Switzerland - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Beside that, whenever you see a reply you think its helpful, mark it as helpful! And whenever you see a reply being an answer to the mainquestion of the thread, mark it as answer (if you opened the thread).
Tuesday, September 22, 2015 1:12 PM
I try to make the parallel whith the "BATCH world", and the "PowerShell World" :
DIR C:\ > d:\toto.txt
How to write this commande in PowerShell ?
I try to use "<powershell_commande> | Write-Log D:\toto.txt", with no success....
As you can see, I'm very interesting to know how to write the result of a command inside a "log file" , using PowerShell.
Tuesday, September 22, 2015 1:13 PM
I try to make the parallel whith the "BATCH world", and the "PowerShell World" :
DIR C:\ > d:\toto.txt
How to write this commande in PowerShell ?I try to use "<powershell_commande> | Write-Log D:\toto.txt", with no success....
As you can see, I'm very interesting to know how to write the result of a command inside a "log file" , using PowerShell.
Please do not necro old answered threads. Start your own if you have questions.
Thursday, September 26, 2019 6:54 PM
"The output stream (stdout) represents the "normal" output of a command with the Cmdlet Write-Host ..."
Write-Host actually does not use stdout or any of the output streams, rather it goes directly to the console. See Scripting Guy's posts on this topic of streams, redirection, and write-host.