Skip to content
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

SqlServerDsc: Using dbatools as a preferred module #1904

Merged
merged 21 commits into from
Apr 24, 2023

Conversation

johlju
Copy link
Member

@johlju johlju commented Apr 9, 2023

Pull Request (PR) description

  • SqlServerDsc
    • Using dbatools as a preferred module.

This Pull Request (PR) fixes the following issues

None.

Task list

  • Added an entry to the change log under the Unreleased section of the
    file CHANGELOG.md. Entry should say what was changed and how that
    affects users (if applicable), and reference the issue being resolved
    (if applicable).
  • Resource documentation updated in the resource's README.md.
  • Resource parameter descriptions updated in schema.mof.
  • Comment-based help updated, including parameter descriptions.
  • Localization strings updated.
  • Examples updated.
  • Unit tests updated. See DSC Community Testing Guidelines.
  • Integration tests updated (where possible). See DSC Community Testing Guidelines.
  • Code changes adheres to DSC Community Style Guidelines.

This change is Reviewable

@codecov
Copy link

codecov bot commented Apr 9, 2023

Codecov Report

Merging #1904 (0c2df20) into main (44c654a) will decrease coverage by 1%.
The diff coverage is 75%.

Impacted file tree graph

@@         Coverage Diff          @@
##           main   #1904   +/-   ##
====================================
- Coverage    92%     92%   -1%     
====================================
  Files        91      91           
  Lines      7725    7732    +7     
====================================
+ Hits       7122    7127    +5     
- Misses      603     605    +2     
Flag Coverage Δ
unit 92% <75%> (-1%) ⬇️
Impacted Files Coverage Δ
source/Public/Get-SqlDscPreferredModule.ps1 97% <75%> (-3%) ⬇️
source/Public/Import-SqlDscPreferredModule.ps1 95% <75%> (-5%) ⬇️

@johlju johlju added the on hold The issue or pull request has been put on hold by a maintainer. label Apr 9, 2023
@johlju johlju force-pushed the f/use-dbatools-as-preferred-module branch from 33f43cf to 50fee59 Compare April 10, 2023 07:18
@potatoqualitee
Copy link

k, it's updated on my end 👍🏼

@johlju
Copy link
Member Author

johlju commented Apr 11, 2023

@potatoqualitee now the build fail on https://dev.azure.com/dsccommunity/SqlServerDsc/_build/results?buildId=7777&view=logs&j=0e1de302-4f5f-5cc3-ef9c-00ac523cd866&t=1c0b58bc-930f-54e7-60af-0caf301f2f32&l=1582

The term 'Read-DbatoolsConfigPersisted' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again

"Cannot find path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\p' because it does not exist."

@potatoqualitee
Copy link

That is the strangest thing and so far I cannot replicate. Do you have a guide for building and testing? I tried to follow along but got lost.

@johlju
Copy link
Member Author

johlju commented Apr 12, 2023

I will see if I can make an easier repro than running the integration tests in CI. I might not have time for a few days though. I let you know. 😊

@johlju johlju force-pushed the f/use-dbatools-as-preferred-module branch from c780509 to b08d606 Compare April 12, 2023 11:29
@johlju
Copy link
Member Author

johlju commented Apr 12, 2023

@potatoqualitee I see a lot of Write-ImportTime in dbatools .psm1 file. Is it possible to get those to output to console?

I wonder if the module tries to do something interactive that is not allowed in the session that LCM runs. I tried to use it outside of the DSC resource and there it imports as expected, so it must have to be something related to running the DSC resources.

@potatoqualitee
Copy link

Yes, you can use Measure-DbatoolsImport. Sometimes I see weird things like that when LF is expected and CRLF is used?

Also, we do use decompression during our import, perhaps that's prohibited?

@johlju
Copy link
Member Author

johlju commented Apr 12, 2023

Sometimes I see weird things like that when LF is expected and CRLF is used?

We always clone the repo with the following .gitattributes file, so it should always use CRLF. 🤔

# Needed for publishing of examples, build worker defaults to core.autocrlf=input.
* text eol=crlf

# Ensure any exe files are treated as binary
*.exe binary
*.jpg binary
*.xl* binary
*.pfx binary

Also, we do use decompression during our import, perhaps that's prohibited?

Unsure. Haven't heard of any restrictions other than interactive. Need to fins out what row it fails on in dbatools when it is imported, so probably have to run a DSC resource in a lab and then fill dbatools .psm1 with verbose messages or something so I can find where it fails 🙂

It feels SqlServerDsc fails here when it tries to import dbatools on the line below. Running the same command in the console it works as expected.

https://github.com/dsccommunity/SqlServerDsc/pull/1904/files#diff-3e3dcb9800c27f7f845abc7ef5d03a35091ddf398eadac8aec442fc260837d76L92

@johlju
Copy link
Member Author

johlju commented Apr 12, 2023

So this failed in a Script resource too, so that will make this more simple to test. No SQL Server resource needed.

I tried removing the message that outputs during import just in case that was preventing it from working.
https://github.com/dsccommunity/SqlServerDsc/pull/1904/files#diff-f77d89f77c1da47ff356e302f4deb1bfe94ba9e172c451195fab9e674b241691R428-R429

It is run just after the module is installed, We can see below that the install worked, but then it failed to find the dbatools command. Probably because the module could not imported like we saw previously.

Ignore that it say InstallSqlServerModule, I only changed the necessary stuff for this PoC, and skipped naming of stuff. 🙂

2023-04-12T11:59:42.6070937Z VERBOSE: [WIN-HRIMNBTGK52]:                            [[Script]InstallSqlServerModule] Installed dbatools module 
2023-04-12T11:59:42.6073736Z version 2.0.0-preview3
2023-04-12T11:59:42.6462476Z VERBOSE: [WIN-HRIMNBTGK52]:                            [[Script]InstallSqlServerModule] Populating 
2023-04-12T11:59:42.6469089Z RepositorySourceLocation property for module dbatools.library.
2023-04-12T11:59:42.6471283Z VERBOSE: [WIN-HRIMNBTGK52]:                            [[Script]InstallSqlServerModule] Loading module from path 
2023-04-12T11:59:42.6472162Z 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psm1'.
2023-04-12T11:59:42.6789274Z VERBOSE: [WIN-HRIMNBTGK52]:                            [[Script]InstallSqlServerModule] Loading module from path 
2023-04-12T11:59:42.6793038Z 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psd1'.
2023-04-12T11:59:42.6839530Z VERBOSE: [WIN-HRIMNBTGK52]:                            [[Script]InstallSqlServerModule] Populating 
2023-04-12T11:59:42.6840289Z RepositorySourceLocation property for module dbatools.library.
2023-04-12T11:59:42.6861133Z VERBOSE: [WIN-HRIMNBTGK52]:                            [[Script]InstallSqlServerModule] Loading module from path 
2023-04-12T11:59:42.6864085Z 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psm1'.
2023-04-12T11:59:42.6987255Z VERBOSE: [WIN-HRIMNBTGK52]:                            [[Script]InstallSqlServerModule] Exporting function 
2023-04-12T11:59:42.6990695Z 'Get-DbatoolsLibraryPath'.
2023-04-12T11:59:42.7002830Z VERBOSE: [WIN-HRIMNBTGK52]:                            [[Script]InstallSqlServerModule] Importing function 
2023-04-12T11:59:42.7003770Z 'Get-DbatoolsLibraryPath'.
2023-04-12T11:59:48.1036474Z VERBOSE: [WIN-HRIMNBTGK52]: LCM:  [ End    Set      ]  [[Script]InstallSqlServerModule]  in 44.0460 seconds.
2023-04-12T11:59:48.3228382Z ##[error]    [-] Should compile and apply the MOF without throwing 46.5s (46.49s|17ms)�[0m
2023-04-12T11:59:48.3243308Z ##[error]     Expected no exception to be thrown, but an exception "PowerShell DSC resource MSFT_ScriptResource  failed to execute Set-TargetResource functionality with error message: System.InvalidOperationException: The set script threw an error. ---> System.Management.Automation.CommandNotFoundException: The term 'Register-DbatoolsConfig' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.�[0m
2023-04-12T11:59:48.3267457Z ##[error]        at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)�[0m
2023-04-12T11:59:48.3329013Z ##[error]        at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)�[0m
2023-04-12T11:59:48.3420039Z ##[error]        at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)�[0m
2023-04-12T11:59:48.3534822Z ##[error]        at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)�[0m
2023-04-12T11:59:48.3554247Z ##[error]        --- End of inner exception stack trace --- " was thrown from D:\a\1\s\tests\Integration\DSC_SqlSetup.Integration.Tests.ps1:247 char:17�[0m
2023-04-12T11:59:48.3585497Z ##[error]         + ...               Start-DscConfiguration @startDscConfigurationParameters�[0m
2023-04-12T11:59:48.3607205Z ##[error]         +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.�[0m

@potatoqualitee
Copy link

okie dok, I removed the compression import. please try now with preview4 (sorry, i dont understand the implication of the Script comment 😅 )

@johlju
Copy link
Member Author

johlju commented Apr 13, 2023

Cool. Will test tomorrow. Script resource just simplifies making a repro, but haven’t gotten that far. Hopefully I have time this weekend to make an easy repro if your latest fix does not work.

@potatoqualitee
Copy link

(I can't see the error right now because I'm on mobile so I'm making assumptions.) Yay, still failed so I won't have to remove the compression 😅 Happy to work with you this weekend to see what's going on.

@johlju
Copy link
Member Author

johlju commented Apr 15, 2023

It also fails in AppVeyor so that rules out the build workers of Azure DevOps and GitHub Actions. I will try to make some time to create an easy repro.

@gaelcolas
Copy link
Member

I know very little of what y'all doing and how dbatools work beyond the title of this issue, but I thought I'd point out that IF dbatools is using ScriptsToProcess to do something before it loads, that won't work in DSC (wmf5.1), and I don't know for Machine Config (but there's bigger chance it works there).

@johlju johlju changed the title SqlServerDsc: Using dbatools as the preferred module SqlServerDsc: Using dbatools as a preferred module Apr 16, 2023
@johlju
Copy link
Member Author

johlju commented Apr 16, 2023

Renamed the title to better describe what this goal is, if possible (this is just a PoC). I wanted to see how far dbatools module works, because it supports assemblies that is not supported (yet) by SqlServer module. It would be opt-in for a user. Though it probably won't be able to be used in other areas since it lack the necessary commands that Sqlserver has - and SqlServer and dbatools might not be able to be used concurrently since both have (or can diverge to) different SMO assemblies since they are sourced from the SMO repository at different times. It would have been better if both dbatools and SqlServer sourced the same assembly versions at the same time, but the Community (@potatoqualitee) is faster here than the SQL Server team those updating SqlServer module (like once a year if we are lucky).

@johlju
Copy link
Member Author

johlju commented Apr 16, 2023

The dbatools module has ScriptToProcess set to @() which maybe still is a problem?

    # Script files () that are run in the caller's environment prior to importing this module
    ScriptsToProcess   = @()

    # Type files (xml) to be loaded when importing this module
    TypesToProcess     = @("xml\dbatools.Types.ps1xml")

    # Format files (xml) to be loaded when importing this module
    # "xml\dbatools.Format.ps1xml"
    FormatsToProcess   = @("xml\dbatools.Format.ps1xml")

@johlju
Copy link
Member Author

johlju commented Apr 16, 2023

I will look into making a repro this morning.

@gaelcolas
Copy link
Member

The dbatools module has ScriptToProcess set to @() which maybe still is a problem?

No, that should be ok.

@johlju
Copy link
Member Author

johlju commented Apr 16, 2023

Repro:

Run this preferably on a lab machine since it changes stuff to be able to run LCM that is normally not configured.

All must be run as elevated user in Windows PowerShell. I ran it on a Windows 11 22H2, but would of course work on server flavors too. I ran it on a clean Windows 11 so there are more steps that might be need but you all know this (just a reminder to myself). 🙂

  1. Allow execution of scripts
Set-ExecutionPolicy RemoteSigned
  1. Install latest PowerShellGet
Install-module PowerShellGet -Scope AllUsers
  1. Start a new PowerShell session to more easily use the latest installed PowerShellGet version.

  2. Install dependent modules.

Install-module PSDscResources -Scope AllUsers -Force
Install-module dbatools -Scope AllUsers -AllowPrerelease -Force
  1. Enable running the LCM. Make sure the network NIC is set to Private or Domain (change from Public if not) otherwise it won't be able to configure firewall rules if it set to Public.
winrm quickconfig -quiet
  1. Start ISE (or other editor that uses Windows PowerShell) and run this script
<# 
    Change method below to:
      Get  - to get output of the latest version of dbatools (that is found)
      Set  - to try to import dbatools
      Test - not used (does nothing)
#>
Invoke-DscResource -Verbose -Name Script -ModuleName PSDscResources -Method Set -Property @{
    SetScript  = {
        $importedModule = Import-Module -Name dbatools -Passthru

        Write-Verbose -Message ('Imported dbatools module version {0}' -f $importedModule.Version)

        Set-DbatoolsConfig -Name Import.EncryptionMessageCheck -Value $false -PassThru |
            Register-DbatoolsConfig -Verbose

        Write-Verbose -Message 'Disabled dbatools setting Import.EncryptionMessageCheck'
    }

    TestScript = {
        return $true
    }

    GetScript  = {
        $moduleVersion = $null
        $sqlServerModule = $null

        $sqlServerModule = Get-Module -Name 'dbatools' -ListAvailable |
            Sort-Object -Property Version -Descending |
            Select-Object -First 1

        if ($sqlServerModule)
        {
            $moduleVersion = $sqlServerModule.Version.ToString()

            if ($sqlServerModule.PrivateData.PSData.Keys -contains 'Prerelease')
            {
                if (-not [System.String]::IsNullOrEmpty($sqlServerModule.PrivateData.PSData.Prerelease))
                {
                    $moduleVersion = '{0}-{1}' -f $moduleVersion, $sqlServerModule.PrivateData.PSData.Prerelease
                }
            }

            Write-Verbose -Message ('Found dbatools module v{0}.' -f $moduleVersion) -Verbose
        }

        return @{
            Result = $moduleVersion
        }
    }
}

@johlju
Copy link
Member Author

johlju commented Apr 16, 2023

Should result in the error being shown in the output

VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = ResourceSet,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredSta
teConfiguration'.
VERBOSE: An LCM method call arrived from computer PC05 with user sid S-1-5-21-2410296128-3233396837-1900027428-1001.
VERBOSE: [PC05]: LCM:  [ Start  Set      ]  [[Script]DirectResourceAccess]
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Begin executing set script.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Executing script: 
        $importedModule = Import-Module -Name dbatools -Passthru

        Write-Verbose -Message ('Imported dbatools module version {0}' -f $importedModule.Version)

        Set-DbatoolsConfig -Name Import.EncryptionMessageCheck -Value $false -PassThru |
            Register-DbatoolsConfig -Verbose

        Write-Verbose -Message 'Disabled dbatools setting Import.EncryptionMessageCheck'
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\dbatools.psd1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Populating RepositorySourceLocation property for module dbatools.library.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psm1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psd1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Populating RepositorySourceLocation property for module dbatools.library.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psm1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Exporting function 'Get-DbatoolsLibraryPath'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Importing function 'Get-DbatoolsLibraryPath'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading 'TypesToProcess' from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\xml\dbatools.Types.ps1xml'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading 'FormatsToProcess' from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\xml\dbatools.Format.ps1xml'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Populating RepositorySourceLocation property for module dbatools.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\dbatools.psm1'.
Cannot find path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\private\f' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (C:\Program File...2.0.0\private\f:) [], CimException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost
 
Cannot find path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\p' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (C:\Program File...batools\2.0.0\p:) [], CimException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost
 
VERBOSE: [PC05]: LCM:  [ End    Set      ]  [[Script]DirectResourceAccess]  in 6.6090 seconds.
An internal error occurred. 
    + CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : HRESULT 0x8007054f
    + PSComputerName        : localhost
 
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 7.579 seconds

@johlju
Copy link
Member Author

johlju commented Apr 16, 2023

It seems it fails on line 206 and 216 in this image

image

See output.

VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG21
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG22
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] True
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.1
Cannot find path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\private\f' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (C:\Program File...2.0.0\private\f:) [], CimException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost
 
Cannot find path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\private\f' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (C:\Program File...2.0.0\private\f:) [], CimException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost
 
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.2
Cannot find path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\p' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (C:\Program File...batools\2.0.0\p:) [], CimException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost
 
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG23
VERBOSE: [PC05]: LCM:  [ End    Set      ]  [[Script]DirectResourceAccess]  in 0.1870 seconds.
An internal error occurred. 
    + CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : HRESULT 0x8007054f
    + PSComputerName        : localhost

If running the command manually if fails with the same error since the folder does not exist, Unclear why it cuts of the full path.

PS > Get-ChildItem -Path "C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0/private/functions/" -Recurse -Filter *.ps1
Get-ChildItem : Cannot find path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\private\f' because it does not exist.
At line:1 char:1
+ Get-ChildItem -Path "C:\Program Files\WindowsPowerShell\Modules\dbato ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Program File...2.0.0\private\f:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

@potatoqualitee The solution should probably be to evaluate if the path exist before trying to fetch files?

@johlju
Copy link
Member Author

johlju commented Apr 16, 2023

Testing for paths passed the errors, using this code:

if (-not (Test-Path -Path "$psScriptRoot\dbatools.ps1") -or $script:serialimport) {
    Write-Verbose -Message 'DEBUG 22.1' -Verbose

    # All internal functions privately available within the toolset
    if ((Test-Path -Path (Join-Path -Path $psScriptRoot -ChildPath 'private/functions')))
    {
        Write-Verbose -Message 'DEBUG 22.1.1' -Verbose
        foreach ($file in (Get-ChildItem -Path "$psScriptRoot/private/functions/" -Recurse -Filter *.ps1)) {
            . $file.FullName
        }
    }

    Write-Verbose -Message 'DEBUG 22.2' -Verbose

    Write-ImportTime -Text "Loading internal commands via dotsource"

    if ((Test-Path -Path "$psScriptRoot/public"))
    {
        # All exported functions
        foreach ($file in (Get-ChildItem -Path "$script:PSModuleRoot/public/" -Recurse -Filter *.ps1)) {
            . $file.FullName
        }
    }

    Write-Verbose -Message 'DEBUG 22.3' -Verbose

    Write-ImportTime -Text "Loading external commands via dotsource"
} else {
    Write-Verbose -Message 'DEBUG 22.4' -Verbose

    Import-Command -Path "$script:PSModuleRoot/dbatools.ps1"
    Write-ImportTime -Text "Loading dbatools.ps1 using Import-Command"

    Write-Verbose -Message 'DEBUG 22.5' -Verbose
}

resulted in

VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG22
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] True
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG23
VERBOSE: [PC05]: LCM:  [ End    Set      ]  [[Script]DirectResourceAccess]  in 0.7740 seconds.
An internal error occurred. 
    + CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : HRESULT 0x8007054f
    + PSComputerName        : localhost

I came passed that error, but now if fails on something else... Gonna see if I can find what it fails on now.

@johlju
Copy link
Member Author

johlju commented Apr 16, 2023

Ah, there a more of the same isuee, justbelow there are one more, that need to check if path exist.

foreach ($file in (Get-ChildItem -File -Path "$psScriptRoot/private/configurations")) {
    Import-Command -Path $file.FullName
}

@johlju
Copy link
Member Author

johlju commented Apr 16, 2023

It also seems the module is import over and over again. The .psm1 is run several times, then it fails.

VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = ResourceSet,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer PC05 with user sid S-1-5-21-2410296128-3233396837-1900027428-1001.
VERBOSE: [PC05]: LCM:  [ Start  Set      ]  [[Script]DirectResourceAccess]
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Begin executing set script.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Executing script: 
        $importedModule = Import-Module -Name dbatools -Passthru

        Write-Verbose -Message ('Imported dbatools module version {0}' -f $importedModule.Version) -Verbose

        #Set-DbatoolsConfig -Name Import.EncryptionMessageCheck -Value $false -PassThru |
        #    Register-DbatoolsConfig -Verbose

        #Write-Verbose -Message 'Disabled dbatools setting Import.EncryptionMessageCheck' -Verbose
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\dbatools.psd1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Populating RepositorySourceLocation property for module dbatools.library.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psm1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psd1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Populating RepositorySourceLocation property for module dbatools.library.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psm1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Exporting function 'Get-DbatoolsLibraryPath'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Importing function 'Get-DbatoolsLibraryPath'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading 'TypesToProcess' from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\xml\dbatools.Types.ps1xml'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading 'FormatsToProcess' from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\xml\dbatools.Format.ps1xml'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Populating RepositorySourceLocation property for module dbatools.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\dbatools.psm1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG4
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG9
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG10
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG11
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG12
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG13
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG13
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG14
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG15
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG16
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG17
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG18
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG19
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG20
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG21
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG22
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] True
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG23
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG24
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG4
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG9
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG10
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG11
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG12
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG13
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG13
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG14
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG15
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG16
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG17
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG18
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG19
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG20
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG21
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG22
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] True
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG23
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG24
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG25
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG26
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG27
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG4
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG9
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG10
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG11
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG12
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG13
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG13
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG14
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG15
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG16
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG17
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG18
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG19
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG20
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG21
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG22
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] True
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG23
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG24
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG4
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG9
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG10
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG11
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG12
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG13
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG13
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG14
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG15
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG16
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG17
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG18
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG19
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG20
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG21
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG22
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] True
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG23
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG24
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG25
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG26
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG27
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG999
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG25
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG26
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG27
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG999
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG999
VERBOSE: [PC05]: LCM:  [ End    Set      ]  [[Script]DirectResourceAccess]  in 1.1970 seconds.
PowerShell DSC resource MSFT_ScriptResource  failed to execute Set-TargetResource functionality with error message: System.InvalidOperationException: The set script threw an error. ---> System.Management.Automation.ActionPreferenceStopException: The running command 
stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The term 'Join-DbaPath' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   --- End of inner exception stack trace --- 
    + CategoryInfo          : InvalidOperation: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : ProviderOperationExecutionFailure
    + PSComputerName        : localhost
 
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 1.473 seconds

@johlju
Copy link
Member Author

johlju commented Apr 16, 2023

Okay, it seems it loads several times because the .psm1 tries to dogfood its own commands before it is fully imported. Removing code that have Dba commands solves that. Commands that should be dogfood maybe need to be in a dbatools.common module or something?

Commented code:

#if (-not ([Dataplat.Dbatools.Message.LogHost]::LoggingPath)) {
#    [Dataplat.Dbatools.Message.LogHost]::LoggingPath = Join-DbaPath $script:AppData "PowerShell" "dbatools"
#}

...

$loadedModuleNames = (Get-Module sqlserver, sqlps -ErrorAction Ignore).Name
if ($loadedModuleNames -contains 'sqlserver' -or $loadedModuleNames -contains 'sqlps') {
    #if (Get-DbatoolsConfigValue -FullName Import.SqlpsCheck) {
    #    Write-Warning -Message 'SQLPS or SqlServer was previously imported during this session. If you encounter weird issues with dbatools, please restart PowerShell, then import dbatools without loading SQLPS or SqlServer first.'
    #    Write-Warning -Message 'To disable this message, type: Set-DbatoolsConfig -Name Import.SqlpsCheck -Value $false -PassThru | Register-DbatoolsConfig'
    #}
}
...

#if (Get-DbatoolsConfigValue -FullName Import.EncryptionMessageCheck) {
if ($false) {
    $trustcert = Get-DbatoolsConfigValue -FullName sql.connection.trustcert
    $encrypt = Get-DbatoolsConfigValue -FullName sql.connection.encrypt
    if (-not $trustcert -or $encrypt -in "Mandatory", "$true") {
        # keep it write-host for psv3
        Write-Message -Level Output -Message '
/   /                                                                     /   /
| O |                                                                     | O |
|   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|   |
...

Set-DbatoolsConfig -Name Import.EncryptionMessageCheck -Value $false -PassThru |
Register-DbatoolsConfig'
    }
}

Then it doesn't try to re-import itself, and fails on another location... digging further.

VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = ResourceSet,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer PC05 with user sid S-1-5-21-2410296128-3233396837-1900027428-1001.
VERBOSE: [PC05]: LCM:  [ Start  Set      ]  [[Script]DirectResourceAccess]
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Begin executing set script.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Executing script: 
        $importedModule = Import-Module -Name dbatools -Passthru

        Write-Verbose -Message ('Imported dbatools module version {0}' -f $importedModule.Version) -Verbose

        #Set-DbatoolsConfig -Name Import.EncryptionMessageCheck -Value $false -PassThru |
        #    Register-DbatoolsConfig -Verbose

        #Write-Verbose -Message 'Disabled dbatools setting Import.EncryptionMessageCheck' -Verbose
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\dbatools.psd1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Populating RepositorySourceLocation property for module dbatools.library.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psm1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psd1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Populating RepositorySourceLocation property for module dbatools.library.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.4.9\dbatools.library.psm1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Exporting function 'Get-DbatoolsLibraryPath'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Importing function 'Get-DbatoolsLibraryPath'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading 'TypesToProcess' from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\xml\dbatools.Types.ps1xml'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading 'FormatsToProcess' from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\xml\dbatools.Format.ps1xml'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Populating RepositorySourceLocation property for module dbatools.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\dbatools.psm1'.
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG4
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG9
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG10
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG11
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG12
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG13
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG13
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG14
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG15
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG16
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG17
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG18
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG19
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG20
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG21
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG22
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] True
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG 22.3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG23
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG24
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG25
VERBOSE: [PC05]: LCM:  [ End    Set      ]  [[Script]DirectResourceAccess]  in 0.7640 seconds.
An internal error occurred. 
    + CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : HRESULT 0x8007054f
    + PSComputerName        : localhost
 
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 1.204 seconds

@johlju
Copy link
Member Author

johlju commented Apr 16, 2023

Adding a try catch statement around this command shows that is too tries to dogfood itself before it is fully imported.

image

VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG25
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG25.1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG25.2
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\private\scripts\insertTepp.ps1
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG25.3
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] 

Exception             : System.Management.Automation.CommandNotFoundException: The term 
                        'Register-DbaTeppArgumentCompleter' is not recognized as the name of a cmdlet, function, script 
                        file, or operable program. Check the spelling of the name, or if a path was included, verify 
                        that the path is correct and try again.
                           at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext 
                        funcContext, Exception exception)
                           at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame 
                        frame)
                           at 
                        System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame 
                        frame)
                           at 
                        System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame 
                        frame)
                           at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
                           at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke 
                        clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 
                        variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object 
                        input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
                           at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.<InvokeWithPipe>b__0()
                           at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThrea
                        dCheck(Action action)
                           at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, 
                        ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object 
                        scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean 
                        propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] 
                        args)
                           at System.Management.Automation.ScriptBlock.DoInvokeReturnAsIs(Boolean useLocalScope, 
                        ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object 
                        scriptThis, Object[] args)
                           at System.Management.Automation.CommandInvocationIntrinsics.InvokeScript(ScriptBlock sb, 
                        Boolean useNewScope, PipelineResultTypes writeToPipeline, IList input, Object[] args)
                           at System.Management.Automation.CommandInvocationIntrinsics.InvokeScript(Boolean 
                        useLocalScope, ScriptBlock scriptBlock, IList input, Object[] args)
                           at Dataplat.Dbatools.Commands.ImportCommand.ProcessRecord()
                           at System.Management.Automation.CommandProcessor.ProcessRecord()
TargetObject          : Register-DbaTeppArgumentCompleter
CategoryInfo          : ObjectNotFound: (Register-DbaTeppArgumentCompleter:String) [Import-Command], 
                        CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException,Dataplat.Dbatools.Commands.ImportCommand
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 13
                        at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\dbatools\2.0.0\dbatools.psm1: line 
                        278
                        at <ScriptBlock>, <No file>: line 2
                        at Invoke-Script, C:\Program Files\WindowsPowerShell\Modules\PSDscResources\2.12.0.0\DscResource
                        s\MSFT_ScriptResource\MSFT_ScriptResource.psm1: line 273
                        at Set-TargetResource, C:\Program Files\WindowsPowerShell\Modules\PSDscResources\2.12.0.0\DscRes
                        ources\MSFT_ScriptResource\MSFT_ScriptResource.psm1: line 142
PipelineIterationInfo : {}
PSMessageDetails      :
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG25.4
VERBOSE: [PC05]:                            [[Script]DirectResourceAccess] DEBUG26

@johlju johlju force-pushed the f/use-dbatools-as-preferred-module branch from 3c30d4d to 6372e2a Compare April 23, 2023 15:39
@johlju johlju added needs review The pull request needs a code review. and removed on hold The issue or pull request has been put on hold by a maintainer. labels Apr 23, 2023
@johlju johlju marked this pull request as ready for review April 23, 2023 16:51
@potatoqualitee
Copy link

oh yeah, it'd be much better to use the SMO equivalents of those queries. Considering so many parameters would have to be added, I agree that going another route would be better.

It's been a pleasure working with you!

@johlju
Copy link
Member Author

johlju commented Apr 24, 2023

I made separate jobs that tests dbatools so that both options are tested. As it turns out it seems SQLPS can coexist with dbatools (at least when dbatools is imported first) so the resource SqlScript and SqlScriptQuery worked an called Invoke-SqlCmd from SQLPS module. Not sure it will work as seemless in a production environment, but happy I didn't have to disable the integration tests - but we still need a new resource I think. I will add a new issues to track that.

All tests pass. Merging this.

@johlju
Copy link
Member Author

johlju commented Apr 24, 2023

It's been a pleasure working with you!

@potatoqualitee Same, been a pleasure!

Thank you for great work in dbatools. 🙂Personally I don't use dbatools as a daily driver, but have colleagues that do. In my current role maintaining this module is the closest to managing SQL Server I come now days 🙂

Copy link
Member Author

@johlju johlju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 7 of 7 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @johlju)

@johlju johlju merged commit 43a080e into dsccommunity:main Apr 24, 2023
@johlju johlju deleted the f/use-dbatools-as-preferred-module branch April 24, 2023 10:25
@potatoqualitee
Copy link

Amazing, merged! 🚀 This is so cool.

I actually hadn't worked much in prod with SQL for a few years but now I'm back to being a DBA and will get to work on dbatools more.

How/when can I share the info about dbatools being an option in SqlServerDsc? Unsure about your release cycle.

@johlju
Copy link
Member Author

johlju commented Apr 24, 2023

How/when can I share the info about dbatools being an option in SqlServerDsc? Unsure about your release cycle.

It is live on the gallery already as a 16.3.0-preview0004. All merges to main are automatically deployed (thanks to the pipeline from the Sampler-project). Though it took a bit longer because the test pipeline failed with intermittent error (due to not enough resources in Azure DevOps) so had to re-run the failed jobs this afternoon.

@johlju
Copy link
Member Author

johlju commented Apr 24, 2023

I will push a full release now and when. I will probably push a full release after PR #1916 is merged since it is a problem for users with SQL 2022.

@johlju johlju removed the needs review The pull request needs a code review. label Apr 24, 2023
@johlju johlju mentioned this pull request May 6, 2023
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants