Giter Site home page Giter Site logo

Comments (9)

YaroBear avatar YaroBear commented on May 29, 2024 1

Awesome, thanks for the help! I’ll see if I can cook up a PR here

from sqlserverdsc.

YaroBear avatar YaroBear commented on May 29, 2024 1

I can incorporate them. Thanks!

from sqlserverdsc.

YaroBear avatar YaroBear commented on May 29, 2024

Maybe something like Import-Module -Name "sqlserver" -RequiredVersion 21.1.18245 -Force after Import-DscResource -ModuleName SqlServerDsc would override the sqlserver module in the current session? For example:

Import-DscResource -ModuleName SqlServerDsc

Import-Module -Name "sqlserver" -RequiredVersion 21.1.18245 -Force

Node $ServerName
    {
        SqlScriptQuery DatabaseSpecs {
        ...

from sqlserverdsc.

kilasuit avatar kilasuit commented on May 29, 2024

It should be possible as per the docs on Import-DSCResource
This though depends on how you are making use of DSC as it's changed lots in recent years

Can you add some more environment details, like is this in Azure, are you using Machine Configuration at all etc etc so that can ensure to give the right advice here.

from sqlserverdsc.

YaroBear avatar YaroBear commented on May 29, 2024

Certainly!

  • We are using Azure Windows Server 2016+ VMs hooked up to Azure Automation Account as our pull server.
  • We use DSC 1.1 and do not use Machine Configuration.

What part of the documentation of Import-DSCResource are you referring to?

Tracing the code from SqlScriptQuery.psm1 -> InvokeSqlScript -> Import-SqlDscPreferredModule is where SqlServer module is imported.
I don't think running Import-Module -Name "sqlserver" -RequiredVersion 21.1.18245 -Force before the resource block will work, because SqlScriptQuery resource is going to override that import before Invoking the Sql script.

from sqlserverdsc.

johlju avatar johlju commented on May 29, 2024

You cannot specify what version of SqlServer to import during compilation, or prior to compilation. As mentioned above the (latest installed) module SqlServer is returned by Get-SqlDscPreferredModule and is then imported by the command Import-SqlDscPreferredModule.

So I think Get-SqlDscPreferredModule must be extended to support a specific version (probably by reading an environment variable $env:SMODefaultModuleVersion) and then make sure Import-SqlDscPreferredModule can import the specific module version returned by Get-SqlDscPreferredModule. I suggest that Get-SqlDscPreferredModule return the module object instead of just the module name.

Happy to review a PR that resolves this.

from sqlserverdsc.

YaroBear avatar YaroBear commented on May 29, 2024

While working on this, I found a bug in these lines of code. The version field isn't getting populated for SQLServer module as expected. Only SQLPS.

I can't tell exactly why. When I'm debugging, the code seems to exit prematurely on line 116 even though all the keys are present. No error codes.

My code changes are modifying this block of code anyway, just thought I'd bring it up.

Here is an output when I run this code on my machine:

Name      Path                                                                                        Version
----      ----                                                                                        -------
SqlServer C:\Users\yaroslav.berejnoi\Documents\PowerShell\Modules\SqlServer\22.0.49\SqlServer.psd1    
SqlServer C:\Users\yaroslav.berejnoi\Documents\PowerShell\Modules\SqlServer\21.1.18256\SqlServer.psd1 
SqlServer C:\Program Files\WindowsPowerShell\Modules\SqlServer\21.1.18256\SqlServer.psd1              
SqlServer C:\Program Files\WindowsPowerShell\Modules\SqlServer\21.1.18245\SqlServer.psd1              
SqlServer C:\Program Files\WindowsPowerShell\Modules\SqlServer\21.1.18147\SqlServer.psd1              
SQLPS     C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS\SQLPS.psd1   130

I tried both PS 7.3.6 and 5.1.19041.3031.

As a quick test for someone else to try, this can simply be copy and pasted into any powershell window. Same code, just with the $Name variable populated:

$Name = @('sqlserver', 'SQLPS')
$availableModule = Get-Module -FullyQualifiedName $Name -ListAvailable |
        Select-Object -Property @(
            'Name',
            'Path',
            @{
                Name       = 'Version'
                Expression = {
                    if ($_.Name -eq 'SQLPS')
                    {
                        <#
                            Parse the build version number '120', '130' from the Path.
                            Older version of SQLPS did not have correct versioning.
                        #>
                        (Select-String -InputObject $_.Path -Pattern '\\([0-9]{3})\\' -List).Matches.Groups[1].Value
                    }
                    else
                    {
                        $versionToReturn = $_.Version

                        if ($_.ContainsKey('PrivateData') -and $_.PrivateData.ContainsKey('PSData') -and $_.PrivateData.PSData.ContainsKey('Prerelease'))
                        {
                            if (-not [System.String]::IsNullOrEmpty($_.PrivateData.PSData.Prerelease))
                            {
                                $versionToReturn = '{0}-{1}' -f $_.Version, $_.PrivateData.PSData.Prerelease
                            }
                        }

                        $versionToReturn
                    }
                }
            }
        )

$availableModule

from sqlserverdsc.

johlju avatar johlju commented on May 29, 2024

It is ContainsKey that throws an error that is not caught for some reason. There were several issues, thanks for catching that.
If you would like I can push these changes, or can you incorporate them in your change? What ever is easiest for you.

Here is the working code:

$Name = @('sqlserver', 'SQLPS')
$availableModule = Get-Module -FullyQualifiedName $Name -ListAvailable |
        Select-Object -Property @(
            'Name',
            'Path',
            @{
                Name       = 'Version'
                Expression = {
                    if ($_.Name -eq 'SQLPS')
                    {
                        <#
                            Parse the build version number '120', '130' from the Path.
                            Older version of SQLPS did not have correct versioning.
                        #>
                        (Select-String -InputObject $_.Path -Pattern '\\([0-9]{3})\\' -List).Matches.Groups[1].Value
                    }
                    else
                    {
                        $versionToReturn = $_.Version.ToString()

                        if ($null -ne $_.PrivateData -and $null -ne $_.PrivateData.PSData -and $null -ne $_.PrivateData.PSData.Prerelease)
                        {
                            if (-not [System.String]::IsNullOrEmpty($_.PrivateData.PSData.Prerelease))
                            {
                                $versionToReturn = '{0}-{1}' -f $versionToReturn, $_.PrivateData.PSData.Prerelease
                            }
                        }

                        $versionToReturn
                    }
                }
            }
        )

$availableModule

This correctly returns

Name      Path                                                                                      Version
----      ----                                                                                      -------
SqlServer C:\Program Files\WindowsPowerShell\Modules\SqlServer\22.0.49\SqlServer.psd1               22.0.49-preview1
SqlServer C:\Program Files\WindowsPowerShell\Modules\SqlServer\21.1.18080\SqlServer.psd1            21.1.18080
SQLPS     C:\Program Files (x86)\Microsoft SQL Server\160\Tools\PowerShell\Modules\SQLPS\SQLPS.psd1 160
SQLPS     C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS\SQLPS.psd1 130

from sqlserverdsc.

johlju avatar johlju commented on May 29, 2024

Hey, I just saw that the line if (-not [System.String]::IsNullOrEmpty($_.PrivateData.PSData.Prerelease)) seems redundant now? We are already checking that it is not null on the if statement above. 🤔  No it is not, the string can be set to empty and then it is not a prerelease, so the line should still be there. 🙂

from sqlserverdsc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.