From be3c785cb143100e00a5f864e5f77e76ba6e848a Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 10 Jun 2023 16:03:29 +0200 Subject: [PATCH] Modify and add commands --- ...t-SqlDscDatabaseEngineInstalledSetting.ps1 | 61 ++++++++++++++ .../Get-SqlDscDatabaseEngineSetting.ps1 | 58 ------------- .../Public/Get-SqlDscInstalledComponent.ps1 | 12 +-- ...scIntegrationServicesInstalledSetting.ps1} | 4 +- ...DscMasterDataServicesInstalledSetting.ps1} | 4 +- .../Test-SqlDscIsDatabaseEngineInstalled.ps1 | 84 +++++++++++++++++++ ...t-SqlDscIsIntegrationServicesInstalled.ps1 | 2 +- ...st-SqlDscIsMasterDataServicesInstalled.ps1 | 4 +- source/en-US/SqlServerDsc.strings.psd1 | 6 +- 9 files changed, 161 insertions(+), 74 deletions(-) create mode 100644 source/Public/Get-SqlDscDatabaseEngineInstalledSetting.ps1 delete mode 100644 source/Public/Get-SqlDscDatabaseEngineSetting.ps1 rename source/Public/{Get-SqlDscIntegrationServicesSetting.ps1 => Get-SqlDscIntegrationServicesInstalledSetting.ps1} (90%) rename source/Public/{Get-SqlDscMasterDataServicesSetting.ps1 => Get-SqlDscMasterDataServicesInstalledSetting.ps1} (93%) create mode 100644 source/Public/Test-SqlDscIsDatabaseEngineInstalled.ps1 diff --git a/source/Public/Get-SqlDscDatabaseEngineInstalledSetting.ps1 b/source/Public/Get-SqlDscDatabaseEngineInstalledSetting.ps1 new file mode 100644 index 000000000..46e843c80 --- /dev/null +++ b/source/Public/Get-SqlDscDatabaseEngineInstalledSetting.ps1 @@ -0,0 +1,61 @@ +<# + .SYNOPSIS + Returns the database engine settings. + + .DESCRIPTION + Returns the database engine settings. + + .PARAMETER InstanceId + Specifies the instance id on which to check if component is installed. + + .OUTPUTS + `[System.Management.Automation.PSCustomObject]` + + .EXAMPLE + Get-SqlDscDatabaseEngineInstalledSetting -InstanceId 'MSSQL13.SQL2016' + + Returns the settings for the database engine. +#> +function Get-SqlDscDatabaseEngineInstalledSetting +{ + [CmdletBinding()] + [OutputType([System.Boolean])] + param + ( + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [System.String] + $InstanceId + ) + + process + { + $databaseEngineSettings = $null + + $getItemPropertyParameters = @{ + Path = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\{0}\Setup' -f $InstanceId + ErrorAction = 'SilentlyContinue' + } + + $setupSettings = Get-ItemProperty @getItemPropertyParameters + + if (-not $setupSettings) + { + $missingDatabaseEngineMessage = $script:localizedData.DatabaseEngineSetting_Get_NotInstalled -f $Version.ToString() + + $writeErrorParameters = @{ + Message = $missingDatabaseEngineMessage + Category = 'InvalidOperation' + ErrorId = 'GISS0001' # cspell: disable-line + TargetObject = $Version + } + + Write-Error @writeErrorParameters + } + else + { + $databaseEngineSettings = [InstalledComponentSetting]::Parse($setupSettings) + } + + return $databaseEngineSettings + } +} diff --git a/source/Public/Get-SqlDscDatabaseEngineSetting.ps1 b/source/Public/Get-SqlDscDatabaseEngineSetting.ps1 deleted file mode 100644 index 472734ca3..000000000 --- a/source/Public/Get-SqlDscDatabaseEngineSetting.ps1 +++ /dev/null @@ -1,58 +0,0 @@ -<# - .SYNOPSIS - Returns the database engine settings. - - .DESCRIPTION - Returns the database engine settings. - - .PARAMETER InstanceId - Specifies the instance id on which to check if component is installed. - - .OUTPUTS - `[System.Management.Automation.PSCustomObject]` - - .EXAMPLE - Get-SqlDscDatabaseEngineSetting -InstanceId 'MSSQL13.SQL2016' - - Returns the settings for the database engine. -#> -function Get-SqlDscDatabaseEngineSetting -{ - [CmdletBinding()] - [OutputType([System.Boolean])] - param - ( - [Parameter(Mandatory = $true)] - [System.String] - $InstanceId - ) - - $databaseEngineSettings = $null - - $getItemPropertyParameters = @{ - Path = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\{0}\Setup' -f $InstanceId - ErrorAction = 'SilentlyContinue' - } - - $setupSettings = Get-ItemProperty @getItemPropertyParameters - - if (-not $setupSettings) - { - $missingDatabaseEngineMessage = $script:localizedData.DatabaseEngineSetting_Get_NotInstalled -f $Version.ToString() - - $writeErrorParameters = @{ - Message = $missingDatabaseEngineMessage - Category = 'InvalidOperation' - ErrorId = 'GISS0001' # cspell: disable-line - TargetObject = $Version - } - - Write-Error @writeErrorParameters - } - else - { - $databaseEngineSettings = [InstalledComponentSetting]::Parse($setupSettings) - } - - return $databaseEngineSettings -} diff --git a/source/Public/Get-SqlDscInstalledComponent.ps1 b/source/Public/Get-SqlDscInstalledComponent.ps1 index 85403aa2b..02b1813e3 100644 --- a/source/Public/Get-SqlDscInstalledComponent.ps1 +++ b/source/Public/Get-SqlDscInstalledComponent.ps1 @@ -120,13 +120,13 @@ function Get-SqlDscInstalledComponent { $databaseLevelVersion = [System.Version] ('{0}.{1}' -f $databaseLevel.Substring(0, 2), $databaseLevel.Substring(2, 1)) - $integrationServicesSettings = Get-SqlDscIntegrationServicesSetting -Version $databaseLevelVersion -ErrorAction 'SilentlyContinue' + $isIntegrationServicesInstalled = Test-SqlDscIsIntegrationServicesInstalled -Version $databaseLevelVersion - if ($integrationServicesSettings) + if ($isIntegrationServicesInstalled) { $installedComponents += [PSCustomObject] @{ Feature = 'IS' - Version = $integrationServicesSettings.Version + Version = $databaseLevelVersion } } @@ -186,13 +186,13 @@ function Get-SqlDscInstalledComponent } # Look for installed version of Master Data Services. - $masterDataServicesSettings = Get-SqlDscMasterDataServicesSetting -Version $databaseLevelVersion -ErrorAction 'SilentlyContinue' + $masterDataServicesSettings = Test-SqlDscIsMasterDataServicesInstalled -Version $databaseLevelVersion if ($masterDataServicesSettings) { $installedComponents += [PSCustomObject] @{ Feature = 'MDS' - Version = $masterDataServicesSettings.Version + Version = $databaseLevelVersion } } @@ -225,7 +225,7 @@ function Get-SqlDscInstalledComponent foreach ($currentInstance in $installedDatabaseEngineInstance) { # Look for installed version of Database Engine. - $databaseEngineSettings = Get-SqlDscDatabaseEngineSetting -InstanceId $currentInstance.InstanceId -ErrorAction 'SilentlyContinue' + $databaseEngineSettings = Get-SqlDscDatabaseEngineInstalledSetting -InstanceId $currentInstance.InstanceId -ErrorAction 'SilentlyContinue' if ($databaseEngineSettings) { diff --git a/source/Public/Get-SqlDscIntegrationServicesSetting.ps1 b/source/Public/Get-SqlDscIntegrationServicesInstalledSetting.ps1 similarity index 90% rename from source/Public/Get-SqlDscIntegrationServicesSetting.ps1 rename to source/Public/Get-SqlDscIntegrationServicesInstalledSetting.ps1 index 96d766e13..4d8e9c26d 100644 --- a/source/Public/Get-SqlDscIntegrationServicesSetting.ps1 +++ b/source/Public/Get-SqlDscIntegrationServicesInstalledSetting.ps1 @@ -12,11 +12,11 @@ `[System.Management.Automation.PSCustomObject]` .EXAMPLE - Get-SqlDscIntegrationServicesSetting -Version ([System.Version] '16.0') + Get-SqlDscIntegrationServicesInstalledSetting -Version ([System.Version] '16.0') Returns the settings for the Integration Services. #> -function Get-SqlDscIntegrationServicesSetting +function Get-SqlDscIntegrationServicesInstalledSetting { [CmdletBinding()] [OutputType([System.Boolean])] diff --git a/source/Public/Get-SqlDscMasterDataServicesSetting.ps1 b/source/Public/Get-SqlDscMasterDataServicesInstalledSetting.ps1 similarity index 93% rename from source/Public/Get-SqlDscMasterDataServicesSetting.ps1 rename to source/Public/Get-SqlDscMasterDataServicesInstalledSetting.ps1 index 3d72f632d..de0872a0f 100644 --- a/source/Public/Get-SqlDscMasterDataServicesSetting.ps1 +++ b/source/Public/Get-SqlDscMasterDataServicesInstalledSetting.ps1 @@ -12,11 +12,11 @@ `[System.Management.Automation.PSCustomObject]` .EXAMPLE - Get-SqlDscIntegrationServicesSetting -Version ([System.Version] '16.0') + Get-SqlDscMasterDataServicesInstalledSetting -Version ([System.Version] '16.0') Returns the settings for the integration services. #> -function Get-SqlDscMasterDataServicesSetting +function Get-SqlDscMasterDataServicesInstalledSetting { [CmdletBinding()] [OutputType([System.Boolean])] diff --git a/source/Public/Test-SqlDscIsDatabaseEngineInstalled.ps1 b/source/Public/Test-SqlDscIsDatabaseEngineInstalled.ps1 new file mode 100644 index 000000000..8d8902349 --- /dev/null +++ b/source/Public/Test-SqlDscIsDatabaseEngineInstalled.ps1 @@ -0,0 +1,84 @@ +<# + .SYNOPSIS + Returns whether the Master Data Services are installed. + + .DESCRIPTION + Returns whether the Master Data Services are installed. + + .PARAMETER Version + Specifies the version for which to check if component is installed. + + .PARAMETER InstanceName + Specifies the instance name on which to check if component is installed. + + .PARAMETER InstanceId + Specifies the instance id on which to check if component is installed. + + .OUTPUTS + [System.Boolean] + + .EXAMPLE + Test-SqlDscIsDatabaseEngineInstalled -Version ([System.Version] '16.0') + + Returns $true if Database Engine with version 16 is installed. + + .NOTES + The parameters are all mutually exclusive. +#> +function Test-SqlDscIsDatabaseEngineInstalled +{ + [CmdletBinding()] + [OutputType([System.Boolean])] + param + ( + [Parameter()] + [System.Version] + $Version, + + [Parameter()] + [ValidateNotNullOrEmpty()] + [System.String] + $InstanceName, + + [Parameter()] + [ValidateNotNullOrEmpty()] + [System.String] + $InstanceId + ) + + $commandParameter = (Remove-CommonParameter -Hashtable $PSCmdlet.MyInvocation.MyCommand.Parameters).Keys + + foreach ($currentParameterName in $commandParameter) + { + $assertBoundParameterParameters = @{ + BoundParameterList = $PSBoundParameters + MutuallyExclusiveList1 = $currentParameterName + MutuallyExclusiveList2 = @([System.String[]] $commandParameter.Where({ $_ -ne $currentParameterName })) + } + + Assert-BoundParameter @assertBoundParameterParameters + } + + $getSqlDscInstalledInstanceParameters = @{ + ServiceType = 'DatabaseEngine' + } + + $installedInstances = Get-SqlDscInstalledInstance @getSqlDscInstalledInstanceParameters -ErrorAction 'SilentlyContinue' + + $result = $false + + if ($PSBoundParameters.ContainsKey('InstanceId')) + { + $result = $installedInstances.InstanceId -contains $InstanceId + } + elseif ($PSBoundParameters.ContainsKey('InstanceName')) + { + $result = $installedInstances.InstanceName -contains $InstanceName + } + elseif ($PSBoundParameters.ContainsKey('Version')) + { + $result = ($installedInstances.InstanceId | Get-SqlDscDatabaseEngineInstalledSetting).Version.Major -contains $Version.Major + } + + return $result +} diff --git a/source/Public/Test-SqlDscIsIntegrationServicesInstalled.ps1 b/source/Public/Test-SqlDscIsIntegrationServicesInstalled.ps1 index d96793846..03e22e631 100644 --- a/source/Public/Test-SqlDscIsIntegrationServicesInstalled.ps1 +++ b/source/Public/Test-SqlDscIsIntegrationServicesInstalled.ps1 @@ -29,7 +29,7 @@ function Test-SqlDscIsIntegrationServicesInstalled $result = $false - if ((Get-SqlDscIntegrationServicesSetting -Version $Version -ErrorAction 'SilentlyContinue')) + if ((Get-SqlDscIntegrationServicesInstalledSetting -Version $Version -ErrorAction 'SilentlyContinue')) { $result = $true } diff --git a/source/Public/Test-SqlDscIsMasterDataServicesInstalled.ps1 b/source/Public/Test-SqlDscIsMasterDataServicesInstalled.ps1 index adea3d4f5..207842e14 100644 --- a/source/Public/Test-SqlDscIsMasterDataServicesInstalled.ps1 +++ b/source/Public/Test-SqlDscIsMasterDataServicesInstalled.ps1 @@ -12,7 +12,7 @@ [System.Boolean] .EXAMPLE - IsMasterDataServicesInstalled -Version ([System.Version] '16.0') + Test-SqlDscIsMasterDataServicesInstalled -Version ([System.Version] '16.0') Returns $true if Master Data Services are installed. #> @@ -29,7 +29,7 @@ function Test-SqlDscIsMasterDataServicesInstalled $result = $false - if ((Get-SqlDscMasterDataServicesSetting -Version $Version -ErrorAction 'SilentlyContinue')) + if ((Get-SqlDscMasterDataServicesInstalledSetting -Version $Version -ErrorAction 'SilentlyContinue')) { $result = $true } diff --git a/source/en-US/SqlServerDsc.strings.psd1 b/source/en-US/SqlServerDsc.strings.psd1 index a73b90976..824ce0109 100644 --- a/source/en-US/SqlServerDsc.strings.psd1 +++ b/source/en-US/SqlServerDsc.strings.psd1 @@ -188,12 +188,12 @@ ConvertFrom-StringData @' ## Test-IsManagementStudioAdvancedInstalled IsManagementStudioAdvancedInstalled_Test_NotSupportedVersion = SQL Server Management Studio Advanced is not supported for the provided version. - ## Get-SqlDscIntegrationServicesSetting + ## Get-SqlDscIntegrationServicesInstalledSetting IntegrationServicesSetting_Get_NotInstalled = There are no Integration Services installed with version {0}. - ## Get-SqlDscMasterDataServicesSetting + ## Get-SqlDscMasterDataServicesInstalledSetting MasterDataServicesSetting_Get_NotInstalled = There are no Master Data Services installed with version {0}. - ## Get-SqlDscDatabaseEngineSetting + ## Get-SqlDscDatabaseEngineInstalledSetting DatabaseEngineSetting_Get_NotInstalled = There are no Database Engine installed with version {0}. '@