Sanjeev
Sanjeev

Reputation: 683

Powershell - Problem with Start-transcript using remoting

I have a file transcript-test.ps1 with below contents

$log="TestLog{0:yyyyMMdd-HHmm}" -f (Get-Date) $logfile = 'C:\logs\'+$log+'.txt' Start-transcript -path $logfile -force Write-host "To test if this message gets logged" Stop-transcript

I try to run the script from lets say "box1" and the log file contains the below contents

********** Windows PowerShell Transcript Start Start time: 20110105114050 Username : domain\user Machine : BOX1 (Microsoft Windows NT 5.2.3790 Service Pack 2) ********** Transcript started, output file is C:\logs\TestLog20110105-1140.txt

To test if this message gets logged

********** Windows PowerShell Transcript End End time: 20110105114050


When I run the same script from another machine using below script I don't see any messages in the log file

Invoke-command {powershell.exe -ExecutionPolicy Unrestricted -NoProfile -File C:\in ll\transcript-test.ps1} -computername box1 -credential $credential get-credential

Contents of log file :

********** Windows PowerShell Transcript Start Start time: 20110105114201 Username : DOMAIN\user Machine : BOX1 (Microsoft Windows NT 5.2.3790 Service Pack 2)


********** Windows PowerShell Transcript End End time: 20110105114201


Is there anyway to log the messages from the script to log file when invoked remotely ?

Thanks! Sanjeev

Upvotes: 0

Views: 1957

Answers (2)

user2099834
user2099834

Reputation: 33

The following will capture your local and remote verbose messages in the transcript

$VerbosePreference = "continue"
Start-Transcript -path c:\temp\transcript.txt 

Write-Verbose "here 1"

$job = Invoke-Command cbwfdev01 -ScriptBlock {

    $ErrorActionPreference = "Stop";
    $VerbosePreference = "continue";

    Write-Verbose "there 1";
    Write-Verbose "there 2";
    Write-Verbose "there 3";

} -AsJob 

Wait-Job $job | Out-Null

$VerboseMessages = $job.ChildJobs[0].verbose.readall()

ForEach ($oneMessage In $VerboseMessages) {Write-Verbose $oneMessage}

Write-Verbose "here 2"

Stop-Transcript

Upvotes: 1

JPBlanc
JPBlanc

Reputation: 72610

Invoke-command execute your code into a job, which is as far as I understant a thread with no console, so no transcription. For me what you got is absolutly normal.

Upvotes: 0

Related Questions