Pergunta

Not sure if this is a simple question or some code is required in order to achieve this, so here it goes.

I have a script which basically displays some text 1st on the PS console and also in a log file.

The code I have uses both the write-host and write-output, so my question is that if the 2 can be combined somehow? I want only specific message to be logged, so doing a PS transcript will not work.

Write-host "Error: whatever message" 
Write-Output "Error: whatever message" | Out-File -Append $Log_file

Thanks in advance!

Foi útil?

Solução

Simple answer is to use a Tee-Object. In version 3 you should have access to the -Append parameter as well (but not in v2)

Write-Output "Error: whatever message" | Tee-Object -Append $Log_file

Outras dicas

Alternatively, you can use Add-Content with -Passthru, and send on to Write-Output from there:

"Error: whatever message" | Add-Content $Log_file -PassThru | Write-Output
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top