dataplat/dbatools

Importing dbatools ends Transcript

shaneis opened this issue · 17 comments

Bug Report

General Troubleshooting steps

  • Verified running latest release of dbatools?
    • dbatools Version 0.9.101
  • Verified errors are not related to permissions?
  • Can duplicate in new/fresh PowerShell.exe session?

Version Information

  • Operating System (Windows 10 Pro | 1703):
  • PowerShell Version: 5.1.15063.632
  • SQL Server (Edition|Version): N/A

Steps to Reproduce

  • Open a new PowerShell.exe shell
  • Start-Transcript
  • Either import explicitly (Import-Module dbatools) or implicilty (Get-Command -Module dbatools)
  • Check transcript (either try and stop it Stop-Transcript, or open the file) will be stopped silently
  • Attach any screenshots (if possible/allowed)
    image

Windows PowerShell transcript start
Start time: 20171122172615
Username: RWS\soneill
RunAs User: RWS\soneill
Machine: 0738-5CG140G7T (Microsoft Windows NT 10.0.15063.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 13232
PSVersion: 5.1.15063.632
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.15063.632
BuildVersion: 10.0.15063.632
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1


Transcript started, output file is .\testing\transcript01.txt
17:26:15
][
00:00:00.0885026
] C:\Users\soneill\Desktop

[import-module dbatools


Windows PowerShell transcript end
End time: 20171122172629


  • Attach logs generated by dbatools (Get-DbatoolsLog)

Message : Registering runspace: dbatools-logging
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:25
FunctionName : Register-DbaRunspace
Level : Verbose
Runspace : 533dff0f-7227-4a46-83b7-6688d759143e
TargetObject : dbatools-logging

Message : Starting runspace: dbatools-logging
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:25
FunctionName : Start-DbaRunspace
Level : Verbose
Runspace : 533dff0f-7227-4a46-83b7-6688d759143e
TargetObject : dbatools-logging

Message : Registering runspace: dbatools-teppasynccache
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:25
FunctionName : Register-DbaRunspace
Level : Verbose
Runspace : 533dff0f-7227-4a46-83b7-6688d759143e
TargetObject : dbatools-teppasynccache

Message : Starting runspace: dbatools-teppasynccache
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:25
FunctionName : Start-DbaRunspace
Level : Verbose
Runspace : 533dff0f-7227-4a46-83b7-6688d759143e
TargetObject : dbatools-teppasynccache

Message : Registering runspace: dbatools-maintenance
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:25
FunctionName : Register-DbaRunspace
Level : Verbose
Runspace : 533dff0f-7227-4a46-83b7-6688d759143e
TargetObject : dbatools-maintenance

Message : Starting runspace: dbatools-maintenance
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:25
FunctionName : Start-DbaRunspace
Level : Verbose
Runspace : 533dff0f-7227-4a46-83b7-6688d759143e
TargetObject : dbatools-maintenance

Message : Updating runspace: dbatools-logging
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:27
FunctionName : Register-DbaRunspace
Level : Verbose
Runspace : e25ac209-02b9-45e3-9acf-a7cb70f01153
TargetObject : dbatools-logging

Message : Starting runspace: dbatools-logging
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:27
FunctionName : Start-DbaRunspace
Level : Verbose
Runspace : e25ac209-02b9-45e3-9acf-a7cb70f01153
TargetObject : dbatools-logging

Message : Updating runspace: dbatools-teppasynccache
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:27
FunctionName : Register-DbaRunspace
Level : Verbose
Runspace : e25ac209-02b9-45e3-9acf-a7cb70f01153
TargetObject : dbatools-teppasynccache

Message : Starting runspace: dbatools-teppasynccache
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:27
FunctionName : Start-DbaRunspace
Level : Verbose
Runspace : e25ac209-02b9-45e3-9acf-a7cb70f01153
TargetObject : dbatools-teppasynccache

Message : Updating runspace: dbatools-maintenance
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:27
FunctionName : Register-DbaRunspace
Level : Verbose
Runspace : e25ac209-02b9-45e3-9acf-a7cb70f01153
TargetObject : dbatools-maintenance

Message : Starting runspace: dbatools-maintenance
Type : Verbose, Debug
Timestamp : 22/11/2017 17:26:27
FunctionName : Start-DbaRunspace
Level : Verbose
Runspace : e25ac209-02b9-45e3-9acf-a7cb70f01153
TargetObject : dbatools-maintenance

  • Attach output from PowerShell console (if possible/allowed)

Feature Request

Problem to solve

Preferably not stop the transcripts.
Or warned that any started transcripts will be ended

Additional information

This is an issue with use of run spaces and @FriedrichWeinmann can answer more but I don't believe this will be something we can fix without removing the run space code utilized to load the module.

This is fixed in PowerShell Core 6.0.0.0, reference issue

So I asked on Twitter and got his response to get the active transcripts which works in this shop

https://twitter.com/SeeminglyScienc/status/933461048329371648

of course restarting it creates a new file so maybe it would be possible to grab the name and append post_dbatools_install(get-Random) to the name ?

`function Get-ActiveTranscriptPath {
[CmdletBinding()]
param()
end {
$flags = [System.Reflection.BindingFlags]'Instance, NonPublic'
$transcriptionData = $Host.Runspace.GetType().
GetProperty('TranscriptionData', $flags).
GetValue($Host.Runspace)

    $transcripts = $transcriptionData.GetType().
        GetProperty('Transcripts', $flags).
        GetValue($transcriptionData)
    
    if (-not $transcripts) {
        return
    }
    
    foreach ($transcript in $transcripts) {
        $transcript.GetType().
            GetProperty('Path', $flags).
            GetValue($transcript)
    }
}

}`

just in case gist goes away

There is also the possibility of restarting the transcript with -Append since we have all the information needed i.e. path the transcript is pointing to, no?

That way there should be no need to start a new one since they can continue the old one.

Very nice @shaneis , I missed the append :-) Not enough coffee

stale commented

This issue has been automatically marked as stale because it has not had recent activity. If you would like this issue to remain open:

  • Verify that you can still reproduce the issue in the latest version of dbatools
  • Comment that the issue is still reproducible and include:
    • What version of dbatools you reproduced the issue on
    • What OS and version you reproduced the issue on
    • What steps you followed to reproduce the issue

This item is still reproducible on 0.9.422. Workaround is to just import the module first

I remember talking to @SQLDBAWithABeard about this when it was first reported.
Something to do with runspaces was what the root cause was, I believe.

There is a way to find out if a transcript has been started so we can restart it after the import, that we got shown from Twitter..
I didn't keep a record of that code though 😢

I've had that experience with runspaces as well. @JustinGrote made a good suggestion to import the module first.

Not sure restarting transcripts is within scope. Will close as won't fix for now. If someone wants to fix and discuss in #dbatools-dev, they are welcomed if it's considered in-scope by a few dbatools devs.

Otherwise, please do import then start transcript.

mmm, actually, i'm making headway with other commands. just not sure i can fix the psm1 import. but i found http://stevegilham.blogspot.com/2017/02/powershell-transcript-cmdlets-and.html

and from that find the following to be effective

$save = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace
# code
[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $save

okay so check this out

No problem:

  1. Import-Module dbatools
  2. Start-Transcript
  3. Run Get-DbaDatabase
  4. Stop-Transcript

No problem:

  1. Start-Transcript
  2. Import-Module dbatools
  3. Stop-Transcript

Problem:

  1. Start-Transcript
  2. Import-Module dbatools
  3. Get-DbaDatabase
  4. Stop-Transcript

not sure whats up, gonna keep digging but it's not looking super promising

I tried a few things but this is out of my league. especially since it appears to occur when a command is run (cuz maybe that initializes a runspace after i set it back in the psm1?)

@FriedrichWeinmann may have some insight since runspaces are his jam, but he's low on time and I won't be able to provide anything further. If anyone else would like to take a look, that'd be fab.

Please let me know if anyone will be taking this on. If not, I'm going to close it with the recommended workaround.

One good thing that came out of this, though, is that a couple of the other commands were fixed. The only thing I can't fix is command running post import.

I'll aim to take this on but not sure how far I can take it without seriously "upping" my PoSh skills...

Just wanted to add another comment to this. I was receiving the transcript error on a restore script (using Restore-DbaDatabase) and could not for the life of me figure out why.

An error occurred stopping transcription: The host is not currently transcribing. Stop-Transcript : An error occurred stopping transcription: The host is not currently transcribing.

So I started googling the error which landed me here.

I am trying the workaround by moving the Import-Module dbatools to before the Start-Transcript and will check back in the AM to see if that resolved my issue.

edit Moving the Start-Transcript to after the Import-Module dbatools fixed my issue.

I was wrong in my previous message. Seems I just needed to give it time to kill the transcript.

image

Gonna agree with @wsmelton's position over a year ago. This is a problem in PS 5.1 that was fixed in PS6. I asked in the referenced issue if it'll be fixed in 5.1 but don't have much hope.

image

image

Hey good news, everyone:

image