dsccommunity / SqlServerDsc

This module contains DSC resources for deployment and configuration of Microsoft SQL Server.
MIT License
360 stars 227 forks source link

SqlPermission: Configuration throws error "The term 'SqlPermission' is not recognized as a name of a cmdlet, function, script file, or executable program" #1908

Closed farzanv closed 1 year ago

farzanv commented 1 year ago

Hello Team,

I tried to use SqlPermission but unfortunately got this error. Looks like it has been removed. Do we have any other functions provide same result?

### The term 'SqlPermission' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of | the name, or if a path was included, verify that the path is correct and try again.

Thanks,

johlju commented 1 year ago

You need to provide more information how you try to use the DSC resource SqlPermission. What are you running to try to accomplish what?

farzanv commented 1 year ago

As per provided example for SQL AG, I followed this link: https://github.com/dsccommunity/SqlServerDsc/blob/main/source/Examples/Resources/SqlAG/1-CreateAvailabilityGroup.ps1

I have installed azure sqlvm, Windows 2022 Datacenter and SQLserver 2022

and here is my part of script.

1- import: image

image 2- configuration

image

When I run the script it works perfectly on other resources such as creating disks and ... but just at this step it fails. image

Please let me know if I have to provide more information.

johlju commented 1 year ago

There are integration tests that run without issue in this repo. Are you running the configuration in Windows PowerShell 5.1? If you run Get-DscResource on the node where SqlServerDsc is installed, do you see the resource? If running in PowerShell 7 then it probably won't work, see issue https://github.com/dsccommunity/DnsServerDsc/issues/268.

farzanv commented 1 year ago

Thank you so much for your prompt reply. Yes I am using PowerShell 5.1 and I can see the resource image

johlju commented 1 year ago

Updated with the correct node so it actually generated the compiled configuration, but it was always parsed correctly.

Very strange. There must be something going on with your machine that I can't figure out. I haven't seen the problem you experience. The only problem I could see that you tried to compile the configuration in PS7, but if you are not doing that I not sure what is going on.

I took the example you linked to and pasted it into ISE (see code snipped below that I used) on one of my lab servers and it ran the configuration (compiled the configuration) without any problem.

image

$configurationData = @{
    AllNodes    = , @{
        NodeName        = 'localhost'
        PSDSCAllowPlainTextPassword = $true
        PSDscAllowDomainUser = $true
    }
}

Configuration Example
{
    param
    (
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $SqlAdministratorCredential
    )

    Import-DscResource -ModuleName 'SqlServerDsc'

    Node localhost
    {
        # Adding the required service account to allow the cluster to log into SQL
        SqlLogin 'AddNTServiceClusSvc'
        {
            Ensure               = 'Present'
            Name                 = 'NT SERVICE\ClusSvc'
            LoginType            = 'WindowsUser'
            ServerName           = $Node.NodeName
            InstanceName         = 'MSSQLSERVER'

            PsDscRunAsCredential = $SqlAdministratorCredential
        }

        # Add the required permissions to the cluster service login
        SqlPermission 'AddNTServiceClusSvcPermissions'
        {
            DependsOn    = '[SqlLogin]AddNTServiceClusSvc'
            ServerName   = $Node.NodeName
            InstanceName = 'MSSQLSERVER'
            Name         = 'NT SERVICE\ClusSvc'
            Credential   = $SqlAdministratorCredential
            Permission   = @(
                ServerPermission
                {
                    State      = 'Grant'
                    Permission = @('AlterAnyAvailabilityGroup', 'ViewServerState')
                }
                ServerPermission
                {
                    State      = 'GrantWithGrant'
                    Permission = @()
                }
                ServerPermission
                {
                    State      = 'Deny'
                    Permission = @()
                }
            )
        }

        # Create a DatabaseMirroring endpoint
        SqlEndpoint 'HADREndpoint'
        {
            EndPointName         = 'HADR'
            EndpointType         = 'DatabaseMirroring'
            Ensure               = 'Present'
            Port                 = 5022
            ServerName           = $Node.NodeName
            InstanceName         = 'MSSQLSERVER'

            PsDscRunAsCredential = $SqlAdministratorCredential
        }

        # Ensure the HADR option is enabled for the instance
        SqlAlwaysOnService 'EnableHADR'
        {
            Ensure               = 'Present'
            InstanceName         = 'MSSQLSERVER'
            ServerName           = $Node.NodeName

            PsDscRunAsCredential = $SqlAdministratorCredential
        }

        # Create the availability group on the instance tagged as the primary replica
        SqlAG 'AddTestAG'
        {
            Ensure               = 'Present'
            Name                 = 'TestAG'
            InstanceName         = 'MSSQLSERVER'
            ServerName           = $Node.NodeName

            DependsOn            = '[SqlAlwaysOnService]EnableHADR', '[SqlEndpoint]HADREndpoint', '[SqlPermission]AddNTServiceClusSvcPermissions'

            PsDscRunAsCredential = $SqlAdministratorCredential
        }
    }
}

$sqlAdministratorCredential = Get-Credential -Message 'User that has permission' -UserName 'sqladmin@dscadlab.com'

Example -SqlAdministratorCredential $sqlAdministratorCredential -OutputPath c:\DSC -ConfigurationData $configurationData
farzanv commented 1 year ago

Thank you so much my issue got fixed.

Resolution, as @johlju mentioned. All modules version for both compiler machines and target nodes must be same and also powershell version needs to be 5.1 on all machines.