dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

PowerShell Studio and DBATools

Open asterix67 opened this issue 4 years ago • 14 comments

Verified issue does not already exist?

No

What error did you receive?

There is the debug :

DEBUG: [11:00:02][Register-DbaRunspace] Registering runspace: dbatools-logging DEBUG: [11:00:02][Start-DbaRunspace] Starting runspace: dbatools-logging DEBUG: [11:00:03][Register-DbaRunspace] Registering runspace: dbatools-teppasynccache DEBUG: [11:00:03][Start-DbaRunspace] Starting runspace: dbatools-teppasynccache DEBUG: [11:00:03][Register-DbaRunspace] Registering runspace: dbatools-maintenance DEBUG: [11:00:03][Start-DbaRunspace] Starting runspace: dbatools-maintenance DEBUG: [11:00:43][Connect-DbaInstance] Starting process block DEBUG: [11:00:50][Connect-DbaInstance] Immediately checking for Azure DEBUG: [11:00:53][Test-Azure] Test for Azure is negative DEBUG: [11:00:55][Connect-DbaInstance] Starting loop for 'VSM-EXPLOIT3': ComputerName = 'VSM-EXPLOIT3', InstanceName = 'MSSQLSERVER', IsLocalHost = 'False', Type = 'Default' DEBUG: [11:00:57][Connect-DbaInstance] String is passed in, will build server object from instance object and other parameters, do some checks and then return the server object DEBUG: [11:00:59][Test-Azure] Test for Azure is negative DEBUG: [11:01:00][Connect-DbaInstance] authentication method is 'local integrated' DEBUG: [11:01:01][Connect-DbaInstance] ApplicationName will be set to 'dbatools PowerShell module - dbatools.io' DEBUG: [11:01:02][Connect-DbaInstance] ConnectionTimeout will be set to '15' DEBUG: [11:01:02][Connect-DbaInstance] Database will be set to 'DB_EXPLOIT' DEBUG: [11:01:03][Connect-DbaInstance] PacketSize will be set to '4096' DEBUG: [11:01:04][Connect-DbaInstance] Pooled will be set to 'True' DEBUG: [11:01:04][Connect-DbaInstance] Building ServerConnection from SqlConnectionInfo DEBUG: [11:01:05][Connect-DbaInstance] ServerConnection was built DEBUG: [11:01:06][Connect-DbaInstance] Building Server from ServerConnection DEBUG: [11:01:07][Connect-DbaInstance] Server was built DEBUG: [11:01:07][Connect-DbaInstance] Setting ConnectionContext.StatementTimeout to '0' DEBUG: [11:01:08][Connect-DbaInstance] The masked server.ConnectionContext.ConnectionString is Data Source='Data Source=VSM-EXPLOIT3;Initial Catalog=DB_EXPLOIT;Integrated Security=True;Multiple Active Result Sets=False;Encrypt=False;Trust Server Certificate=False;Packet Size=4096;Application Name="dbatools PowerShell module - dbatools.io"';Integrated Security=True;Multiple Active Result Sets=False;Encrypt=False;Trust Server Certificate=False;Application Name="SQL Management" DEBUG: [11:01:09][Connect-DbaInstance] We connect to the instance by running SELECT 'dbatools is opening a new connection' DEBUG: [11:01:10][Connect-DbaInstance] Failure | Error connecting to [VSM-EXPLOIT3]: The value's length for key 'data source' exceeds it's limit of '128'.

Steps to Reproduce

I'm using PowerShell Studio 22. When I upgraded module DBATool to the last version, the functions don't work.

Some sample functions I tried :

  • Invoke-DbaQuery -SqlInstance 'VSM-EXPLOIT3' -Database 'MyDB' -Query 'SELECT namedb FROM MyTable' -EnableException
  • Get-DbaDatabase -SqlInstance 'VSM-EXPLOIT3' -ExcludeSystem -OnlyAccessible -EnableException
  • Read-DbaBackupHeader -SqlInstance 'VSM-EXPLOIT3' -Path 'Y:\vsm-exploit2\VSM-EXPLOIT3\DB_EXPLOIT\FULL\DB_EXPLOIT_20220209_100656_FULL.bak' -Simple
  • Get-DbaBackupInformation -SqlInstance 'VSM-EXPLOIT3' -Path 'Y:\vsm-exploit2\VSM-EXPLOIT3\DB_EXPLOIT' -DatabaseName DB_EXPLOIT -MaintenanceSolution

With old version it worked (sample version 1.0.0)

Are you running the latest release?

Yes

Other details or mentions

No response

What PowerShell host was used when producing this error

Windows PowerShell (powershell.exe)

PowerShell Host Version

Name Value


PSVersion 5.1.17763.2268
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.2268
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

SQL Server Edition and Build number

All versions

.NET Framework Version

4.8.03761

asterix67 avatar Feb 11 '22 10:02 asterix67

Some more info from slack:

Commands work with plain powershell (outside of PowerShell Studio).

Reason for the error is that the correct connection string (Data Source=VSM-EXPLOIT3;Initial Catalog=DB_EXPLOIT;Integrated Security=True;Multiple Active Result Sets=False;Encrypt=False;Trust Server Certificate=False;Packet Size=4096;Application Name="dbatools PowerShell module - dbatools.io") is only the "data source"-part of the acctual connection string.

Maybe a hint: Application Name="SQL Management" - who set's this as the default?

I've added some more debug-lines that may help in #8169.

@asterix67 If you are able to change the "allcommands.ps1" (important: Update this file as this is used. The individual files are not used on normal import of the module) you can try to add the new lines and give us the new debug output. Because they all start with "VeryDebug" you can find out if you run the new code.

andreasjordan avatar Feb 11 '22 11:02 andreasjordan

@asterix67 As it works outside of PowerShell Studio - maybe you also ask Sapien for help?

andreasjordan avatar Feb 11 '22 11:02 andreasjordan

If you tried with the DBATools version 1.1.8, it's work in PowerShell Studio. Since version 1.1.9, it doesn't work.

asterix67 avatar Feb 11 '22 12:02 asterix67

There are only two changes in https://github.com/dataplat/dbatools/releases/tag/v1.1.9:

  • #7677
  • #7678

@potatoqualitee - do you see a connection to this issue?

andreasjordan avatar Feb 11 '22 12:02 andreasjordan

PS Studio has it's own host within the tool (just like VS Code does). Only a few folks on the project I think still use that tool to debug it.

wsmelton avatar Feb 11 '22 15:02 wsmelton

Ok. I'll write SAPIEN. Thank you.

asterix67 avatar Feb 14 '22 07:02 asterix67

Hello,

I wrote to SAPIEN.

Here, them message : `The error message comes from the module, not from PowerShell Studio or PowerShell itself.

We are not limiting or modifying any data structures or sizes within the module. We never modify anything in any module, so you will need to contact the vendor of the module about this.

I only pointed out to try the previous version because I remembered that another customer saw the very same error message and indicated that he solved it by using the previous version. It is of course up to you if you want to try that or not.`

I'm alone.

asterix67 avatar Feb 15 '22 10:02 asterix67

I don't have PowerShell Studio. So I can not reproduce the issue. So I can not analyse the problem. So I can not provide a fix.

We release very often, so there are sometimes only small changes. If you can tell us exactly what version is the last that works and the first that does not work - we might have a chance to identify the problem.

Please have a look at the branch of #8169 - if you are able to send me the new debug output, maybe we get a step closer.

andreasjordan avatar Feb 15 '22 10:02 andreasjordan

Thank you.

I give you the versions and the debug (in both version) in the branch of #8169.

asterix67 avatar Feb 15 '22 10:02 asterix67

@FriedrichWeinmann lived in PS Studio at one point and authored most of his work in the module from it.

He is only one I know that may still be using it @potatoqualitee

wsmelton avatar Feb 15 '22 13:02 wsmelton

User feedback is mostly in the linked pull request, but I would like to summarize here.

Root cause was the combination of #7677 and the user script that used [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null to load "C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.SqlServer.Smo\v4.0_15.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Smo.dll" before importing dbatools.

So this can hit us again.

Should we check at some point, if the loaded assemblies are from the dbatools folder or some other place and warn the user?

And just for future reference my new debug lines (as the branch will be deleted):

Write-Message -Level Debug -Message "VeryDebug: serverConnection is from assembly '$($serverConnection.GetType().Assembly.FullName)' at '$($serverConnection.GetType().Assembly.Location)'"
Write-Message -Level Debug -Message "VeryDebug: server is from assembly '$($server.GetType().Assembly.FullName)' at '$($server.GetType().Assembly.Location)'"

And the code that showed the problem:

Import-Module -Name dbatools

$serverName = 'XXXXXXX'
$sqlConnectionInfo = New-Object -TypeName Microsoft.SqlServer.Management.Common.SqlConnectionInfo -ArgumentList $serverName
$serverConnection = New-Object -TypeName Microsoft.SqlServer.Management.Common.ServerConnection -ArgumentList $sqlConnectionInfo
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $serverConnection
$null = $server.ConnectionContext.ExecuteWithResults("SELECT 'dbatools is opening a new connection'")
$server.NetName

$serverConnection.GetType().Assembly.FullName
$serverConnection.GetType().Assembly.Location

$server.GetType().Assembly.FullName
$server.GetType().Assembly.Location

andreasjordan avatar Feb 16 '22 09:02 andreasjordan

excellent writeup! the check could be added to the psm1 so that it happens just once. we already do a check for sqlps there and can add it around that area.

potatoqualitee avatar Feb 16 '22 09:02 potatoqualitee

Ok. Thank you for your help. I think I can close this issue

asterix67 avatar Feb 16 '22 11:02 asterix67

We should be explicitly loading that DLLs from our path.

That should then on import cause an error if sqlps or sqlserver module already loaded it.

wsmelton avatar Feb 16 '22 11:02 wsmelton