0
votes

I am writing a VB.NET PowerShell Cmdlet which needs to start, and later stop, the Transcript file (Start-Transcript). So how do I manage to run the command Start-Transcript from within the cmdlet? I have tried this:

Dim myRunSpace As Runspace = RunspaceFactory.CreateRunspace()
myRunSpace.Open()
Dim MyPipeline As Pipeline = myRunSpace.CreatePipeline()
MyPipeline.Commands.AddScript("Start-Transcript -Path $pwd\session.txt")
Dim results As Collection(Of PSObject) = MyPipeline.Invoke()
myRunSpace.Close()

Produces the error "Start-transcript : this host does not support transription". Yet when I enter the command manually no error is produced and transcription starts.

3

3 Answers

0
votes

The Start-Transcript method is host specific and implemented by the host. The reason for this, I'm guessing, is that it is not clear what input and output is available on different hosts and therefore not easy to create a generic implementation which works in all cases.

The PowerShell Console host supports the Start-Transcript function (which you see, since it works when you call it), but other hosts need to implement it before it is usable in that host. If you, for example, try to write Start-Transcript in the PowerShell ISE host you'll get an error like the following:

Start-Transcript : This host does not support transcription.
At line:1 char:1
+ Start-Transcript
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [Start-Transcript], PSNotSupportedExce 
   ption
    + FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.StartTranscript 
   Command

In order to get it to work using a custom host such as the one you're creating in VB you would have to find a way to implement support for transcription.

0
votes

That is a limitation of the cmdlet. MSFT made it so Start-Transcript only works when invoked from powershell.exe console, it doesn't even work from their ISE. The workaround has been to use add-content / out-file / tee-object to create your own log file.

It's a lot more work, but can be done. maybe these cmdlets can help.

http://www.powertheshell.com/transcript/

0
votes

I have followed one of your earlier links and patched this VB.NET code from the PowerShell script there:

 Dim thisType As Object = MyBase.Host.GetType().GetProperty("ExternalHost",Reflection.BindingFlags.NonPublic Or BindingFlags.Instance).GetValue(MyBase.Host, New Object() {})
 Dim abc As Object = thisType.GetType().GetProperty("IsTranscribing", Reflection.BindingFlags.NonPublic Or BindingFlags.Instance).GetValue(thisType, New Object() {})
 WriteObject(abc.ToString)

This returns correctly True or False. So can I do any more than this here, ie like Stop-Transcript? Apologies for the comment above, but this link is slow and the text became garbled.