Share via


How to redirect verbose to the log file...

Question

Tuesday, May 14, 2013 2:22 PM

Test1.ps1 has below code.....

## Calling script to spool the verbose log to a log file ##

$ScriptPath = Split-Path $MyInvocation.MyCommand.Path
Invoke-Expression "& `"$ScriptPath\Test2.ps1`" $InstallPath $BackupPathApplication"

Now I want to redirect/append output to the log file...Not sure how to pass that in the invoke-expression?

Invoke-Expression "& `"$ScriptPath\Test2.ps1`" $InstallPath $BackupPathApplication >> Logfile.log" ?

All replies (7)

Wednesday, May 15, 2013 4:31 AM ✅Answered

I don't know about doing it with invoke-expression, but if you invoke PowerShell again to run the script, you can capture all the output including verbose output.

In other words, Test1.ps1 would include:

$ScriptPath = Split-Path $MyInvocation.MyCommand.Path
PowerShell $ScriptPath\Test2.ps1 >> Logfile.log


Tuesday, May 14, 2013 2:32 PM

Start-Transcript might be one way. Refer to this link, please.


Tuesday, May 14, 2013 2:52 PM

Hi Wizend,

Test1.ps1 has below code.....

## Calling script to spool the verbose log to a log file ##

$ScriptPath = Split-Path $MyInvocation.MyCommand.Path
$cmd= "& `"$ScriptPath\Test2.ps1`" $InstallPath $BackupPathApplication >> Logfile.log"

Invoke-Expression $cmd

Test2.ps1 has below code.....

write "TEST"

Copy-Item -Path $InstallPath\ -Destination $BackupPathApplication  -Force -Recurse  –Verbose

Output.....

Has TEST in the log file but nothing from the copy-item verbose. Any idea?


Tuesday, May 14, 2013 4:07 PM

Below is working from the powershell prompt...But not by calling Invoke-Expression within Test1.ps1. Please help....

powershell.exe Test1.ps1 >>  Logfile.log


Wednesday, May 15, 2013 8:47 AM

If you want to send the output of your verbose  Copy-Item cmdlet to a file, you should apply the more specialised redirection operator '4> <yourFileWithPath>' to the verbose command itself:

Test2.ps1:
$logfile = <yourLogFileWithPaht>
write "TEST"
Copy-Item -Path $InstallPath\ -Destination $BackupPathApplication  -Force -Recurse  –Verbose 4> $logfile

wizend


Wednesday, May 15, 2013 7:41 PM

@Wizend,

Below option is available only in powershell 3.0? I am using powershell 2.0.

Copy-Item -Path $InstallPath\ -Destination $BackupPathApplication  -Force -Recurse  –Verbose 4> $logfile


Wednesday, May 15, 2013 7:52 PM

I see, Thank you for your response,

Greetings,

wizend