Reputation: 915
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
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
Reputation: 14126
Replacing Write-Host
with Write-Output
did the trick for me on 2012 R2.
Upvotes: 1
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
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