-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SqlDatabaseUser: PowerShell DSC does not support execution of commands in an interactive mode #1647
Comments
It must be this that throws an error SqlServerDsc/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 Lines 365 to 367 in fdc7608
Maybe there is a bug in the helper function |
The error message |
@ArjenB96 you say you use a fresh sql Install. This resource does not create a server login, only a database user. |
Hi @Fiander, My apologies for not being more detailed about that. I actually run my configuration through Ansible with the Win_dsc module, so it is a bit more complex to simply include the additional config.
All steps until step 11 are successful and indicate a change when run for the first time. |
We should bre able to reproduce this with integration tests. Creating the same local user, sql login, and sql database user. @ArjenB96 could you try this manually to verify that `Invoke-Query' is the problem? On the target node that gives the error run this (I haven't tested it myself): # Make sure to run this as the same user as the config uses.
# Change to full path to the module SqlServerDsc.Common inside module SqlServerDsc.
Import-Module -Name .\SqlServerDsc\Modules\SqlServerDsc.Common
$Name = 'TESTBOX\dboperator'
$LoginName = 'TESTBOX\dboperator'
$invokeQueryParameters = @{
ServerName = $env:COMPUTERNAME
InstanceName = 'MSSQLSERVER'
Database = 'TESTENVDB'
}
Invoke-Query @invokeQueryParameters -Verbose -Query (
'CREATE USER [{0}] FOR LOGIN [{1}];' -f $Name, $LoginName
) |
Hmm, I didn't know it was an external command. What module is supposed to install this command? I tried looking for the command and came across the InvokeQuery Ps Module, but installing the newest version (1.0.1) does not provide me with a general Invoke-Query commands, only specific commands such as Invoke-SqlServerQuery. |
then i think you missed the It should be in that module . |
Ah my apologies. I changed the folder for that command, but never actually executed it when running it line-by-line.
This is the error that is given after running the Invoke-Query command on a Powershell instance with the correct user. Might be possible that the barebones installation (only SQLENGINE as feature) of SQL Server 2019 does not bring SMO functionality? |
oke, SMO is not loaded. |
@ArjenB96 you should run this on the target node once it fails so that all prerequisites are installed. |
@johlju I am running this directly on the target node, with proper RunAs credentials to match the PsDscCredentials vars in Dsc config. @Fiander Apparently, since SQL Server 2017 SMO is not installable as a feature through the standard setup. It is now distributed as a NuGet package. The problem with NuGet in this case is that it only works project-wide, so I am not sure how I would apply that to a Dsc config. I guess I can test it for now for the simple PS script to see if it works with SMO installed. |
Installed SMO with nuget.exe into
|
SMO is either part of the PowerShell module SQLPS as part of SQL Server setup or the PowerShell module SqlServer. For SQL Server 2019 I think SQLPS is no longer part of setup, in that case the SqlServer module need to be downloaded from PowerShell Gallery and added to the node to get the correct SMO version as it is distributed inside that module. There should be a verbose message saying it loads the correct module (SqlServer). 🤔 |
Ah, that might be the issue. I have made numerous changes, but when I added the SQLServer PS Module, it worked! I'll narrow it down to make sure. |
Reran the configuration today with the SqlServer module. Still the same SMO TypeNotFound error. Using EDIT: After manually loading the assemblies, it worked. "If your script references a SMO object before the first command that references the provider or cmdlets from the SQL Server snap-ins." For some reason SqlServer/SMO is not automatically loaded / dynamically imported. |
Hi there, just wanted to ask if this is an issue that should be handled on my side or on the resource's side? Due to using Ansible, there's an additional layer of abstraction when incorporating DSC in it. For now, I made a workaround by making Ansible execute a PowerShell script that manually loads the assemblies and executes this DSC resource, but I would rather just install the prereqs, define my DSC config in Ansible and run it like that. |
@johlju is it possible/desirable to bake in the |
@ArjenB96 the The flow to import SMO libraries.
@gaelcolas I would be possible to add module SqlServer (if we are allowed license wise since it bundles proprietary code), but it is not desirable. DSC module SqlServerDsc is not dependent on a specific version of module SqlServer and a user should be able to upgrade to a newer version or pin a certain version of module SqlServer. For example if module SqlServer has breaking changes for a certain major version of Microsoft SQL Server or a new version of Microsoft SQL Server is released that a newer version of module SqlServer supports (we do not have to change the SqlServerDsc in those cases). Also, there might be circumstances where a user are not allowed to use the module from PowerShell Gallery and need to use SQLPS that is part of certain versions of Microsoft SQL Server. A user can use DSC resource |
@ArjenB96 The resource SqlDatabaseUser calls
Love to see the verbose output from the resource to see what is going on. To figure out why you need to load the assemblies manually. |
Here is the verbose output from the
Running it through the DSC resource suddenly works. I am very confused. It now correctly does import SQLServer if I look at the verbose logs, even in a new powershell instance where i did not manually load the binaries. Somehow, running it through Ansible does give me the original error output as shown in my OP, even though the passed values are exactly the same. |
Here is the DSC-related output from Ansible. It seems that it does not execute the query?
If I execute a DSC file directly on the Windows host I get the following additional output:
Seems like for some reason it crashes when trying to find the SqlServer for the second time? |
I'm looking at the code, and i think i have found it. i am missing in what database the login should be created. might work better? SMO is being used to read if a Login exists in a database. @johlju do you agree? |
@ArjenB96 This row says that the module SqlServer have already be imported into the session, so then the SMO assemblies should also have been loaded.
@Fiander the database is set in the splatted parameters and then the query is executed on the database SqlServerDsc/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 Lines 219 to 224 in f1b15bf
|
@ArjenB96 This is the helper module inside SqlServerDsc that fails (SqlServerDsc.Common) becuase it reference a type that module SqlServer loads. For some reason the module SqlServer is not available in the session at that time. But is a non issue now that DSC actually works. 🤔
|
This is strange, it says it does it
But then the test does not seem to find it when testing.
Maybe it's an issue that the database scope as per @Fiander suggest is not set? If so where does it create the user, in the master database (default database for the user)? |
@ArjenB96 Looking at the integration tests for this there is a line missing in your verbose output.
|
I'm scratching my head here. The line in your verbose output from Ansible:
That is created by the SqlServerDsc/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 Lines 351 to 402 in f1b15bf
|
@ArjenB96 can you run SQL Profiler on the instance to see if that SQL statement is ever run and if so in what database? |
@ArjenB96 could you also please share the script you use to create the database? |
@Fiander I create the SQL server and database through Ansible as well, using the win_dsc module with the
|
@ArjenB96 Just wanted to make sure, the database is not in "Containment=partial". can you please confirm? you said "when i add the login through ssms i works." could you please script that action? When i do that, i get a
|
I've checked the config for the database, since I saw the requirement on the wiki for this. The database is not in partial containment mode. Even though we do not explicitly say which database to use, the SQL Profiler shows that it autoselects the correct database before executing the batch query. I have tested it so far only with the Invoke-Query script and loaded assemblies. I will try to test it now through Ansible. |
the reason i ask for a scripted version of the ssms code, is because you run SQL2019. at this moment i only have a few SQL2016 and 2017 servers running |
I'm not sure what exactly you mean by scripting the manual action I do through SSMS. When running through Ansible, it does some miscellaneous stuff, retrieves the proper database, selects the SQL user according to the passed name, but then does not do anything else. That is the last call that is logged. |
Ahh, right, my bad. I add two users to the SQL instance, a db_owner and a db_operator.
|
no, ten days ago you mentioned: "Manually adding the user through SSMS does work without any hiccups." script to new query window. i want to make sure that for SQL2019 nothing changed |
is "TESTBOX" your domain, or a windows machine name? |
Ahh ok. I just shutdown the environment so I'll rebuild it on Wednesday and share the result of the script.
Domain. It's actually something different but I used a dummy name for anonymization. |
I have experienced the same problem. Downgrading to version 13.5.0 of SqlServerDsc has resolved the issue for me. |
… commands in an interactive mode dsccommunity#1647 $servername is not passed thru @PSBoundParameters because the default is used and it is not explicitly set by the caller
Workaround: Explicitly set ServerName on the SqlDatabaseUser resource. |
… commands in an interactive mode dsccommunity#1647 $servername is not passed thru @PSBoundParameters because the default is used and it is not explicitly set by the caller
- SqlDatabaseUser - Added parameter `ServerName` to the call of `Assert-SqlLogin`. `@PSBoundParameters` doesn't capture the default value of `ServerName` when it is not explicitly set by the caller (issue #1647).
Pushing a new release during the day that will have a fix to this thanks to @bschapendonk. 🙂 |
Glad to be of help, also I want to thank @daanroeterink, for the extra set of eyes in tracking this down. |
Details of the scenario you tried and the problem that is occurring
The DatabaseUser resource fails every time when trying to add a new Windows-authenticated user to the database of a fresh MSSQL Server 2019 install. Manually adding the user through SSMS does work without any hiccups.
Verbose logs showing the problem
Suggested solution to the issue
No idea? Where is the interactive window popping up? Can't seem to find any log that explains this.
The DSC configuration that is used to reproduce the issue (as detailed as possible)
I run this script with configuration data that allows plain text passwords saved in a var with Get-Credential:
SQL Server edition and version the target node is running
SQL Server 2019 / 15.0.2000.5
SQL Server PowerShell modules present on the target node
SQLPS 15.0 is the output.
The operating system the target node is running
Version and build of PowerShell the target node is running
PSVersion 5.1.14393.3866
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.3866
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Version of the DSC module that was used
15.0.0
The text was updated successfully, but these errors were encountered: