5

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

Gaurav
  • 895
  • 3
  • 14
  • 31
  • Related: http://stackoverflow.com/questions/13160759/start-transcript-not-capturing-all-output-to-log-file – Matt Mar 16 '15 at 19:35
  • This refers to a known bug in certain builds https://social.technet.microsoft.com/Forums/windowsserver/en-US/cecc4f32-28c8-4bdc-be63-49ce3d396625/powershell-4-starttranscript-does-not-log-writehost – Matt Mar 16 '15 at 19:36
  • Thanks @Matt. The solution mentioned in msdn link is to use Write-Ouput instead of Write-Host which only works for "Local Log"...I am not able to get the "Remote Log" in the transcript while I used to get with previous version of Powershell – Gaurav Mar 16 '15 at 20:03
  • The Technet link also refers to a bug in certain builds of 4.0 if you kept reading. Wondered if that might lead you in the right direction as well. – Matt Mar 16 '15 at 20:05
  • The link does talk about this being a bug and being worked upon by Microsoft team...However I could not find a solution for getting the remote logs in transcript...I was hoping to find a solution / workaround similar to local logs being transcribed...I am using Powershell remoting extensively to interact with Azure VMs and not being able to capture those logs is severely limiting...Thanks again @Matt for your response. – Gaurav Mar 16 '15 at 20:13

3 Answers3

2

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.

Community
  • 1
  • 1
PlantationGator
  • 815
  • 1
  • 13
  • 21
1

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

Taran
  • 12,822
  • 3
  • 43
  • 47
0

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
zerocukor287
  • 555
  • 2
  • 8
  • 23