Gaurav
Gaurav

Reputation: 915

Powershell 4.0 Transcript is not capturing output of Write-Host statements

I have created following script(test.ps1) and I am executing it from command line as "powershell .\test.ps1"

Write-Host(Start-Transcript -Path "D:\logs.txt")
$remoteScript = {
    Write-Host "Remote Log"
}
Invoke-Command -ConnectionUri $uri -Credential $creds -ScriptBlock $remoteScript
Write-Host "Local Log"
Write-Host(Stop-Transcript)

However in the log file generated after executing script, I do not see the log statement either remote or local. This used to work with Powershell 3.0 but recently I upgraded to Powershell 4.0 and it stopped working.

Has anyone faced similar issue or is aware of any other way to capture output from remote and local commands?

Thanks,

Gaurav

Upvotes: 5

Views: 5858

Answers (3)

chancoscu
chancoscu

Reputation: 1

In my case, I use out-host instead of write-host to solve the problem. My powershell information is below

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.34014
BuildVersion                   6.3.9600.17400
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2

Upvotes: 0

Taran
Taran

Reputation: 14126

Replacing Write-Host with Write-Output did the trick for me on 2012 R2.

Upvotes: 1

PlantationGator
PlantationGator

Reputation: 875

Here is a hotfix from Microsoft to resolve this issue:

https://support.microsoft.com/en-us/kb/3014136

It is also discussed here, in Technet

https://social.technet.microsoft.com/Forums/windowsserver/en-US/cecc4f32-28c8-4bdc-be63-49ce3d396625/powershell-4-starttranscript-does-not-log-writehost?forum=winserverpowershell

From the Hotfix site:

On a server that's running Windows Server 2012 R2, you encounter one or more of the following issues when you use PowerShell:

  • Issue 1

The Start-Transcript cmdlet does not capture write-host calls, as seen in the following script example:

Start-Transcript -path $env:TEMP\transcript.txt
Write-Host Hello World
Stop-Transcript

Get-Content $env:TEMP\transcript.txt
In this case, "Hello World" does not appear in the transcript.txt file as expected.

Upvotes: 2

Related Questions