-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get-SqlDscPreferredModule
: New command (#1898)
- SqlServerDsc - New public command: - `Get-SqlDscPreferredModule` - Returns the name of the first available preferred module (issue #1879). - `Import-SqlDscPreferredModule` - Refactor to re-use the command `Get-SqlDscPreferredModule`.
- Loading branch information
Showing
6 changed files
with
617 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
<# | ||
.SYNOPSIS | ||
Get the first available (preferred) module that is installed. | ||
.DESCRIPTION | ||
Get the first available (preferred) module that is installed. | ||
.PARAMETER Name | ||
Specifies the list of the (preferred) modules to search for, in order. | ||
Defaults to 'SqlServer' and then 'SQLPS'. | ||
.PARAMETER Refresh | ||
Specifies if the session environment variable PSModulePath should be refresh | ||
with the paths from other environment variable targets (Machine and User). | ||
.EXAMPLE | ||
Get-SqlDscPreferredModule | ||
Returns the module name SqlServer if it is installed, otherwise it will | ||
return SQLPS if is is installed. If neither is installed `$null` is | ||
returned. | ||
.EXAMPLE | ||
Get-SqlDscPreferredModule -Refresh | ||
Updated the session environment variable PSModulePath and then returns the | ||
module name SqlServer if it is installed, otherwise it will return SQLPS | ||
if is is installed. If neither is installed `$null` is returned. | ||
.EXAMPLE | ||
Get-SqlDscPreferredModule -Name @('MyModule', 'SQLPS') | ||
Returns the module name MyModule if it is installed, otherwise it will | ||
return SQLPS if is is installed. If neither is installed `$null` is | ||
returned. | ||
.NOTES | ||
If the module SQLPS is specified (default value) the path is returned as | ||
the module name. This is because importing 'SQLPS' using simply the name | ||
could make the wrong version to be imported when several different version | ||
of SQL Server is installed on the same node. To make sure the correct | ||
(latest) version is imported the path to the latest version of SQLPS is | ||
returned. The returned path can be passed directly to the parameter Name | ||
of the command Import-Module. | ||
#> | ||
function Get-SqlDscPreferredModule | ||
{ | ||
[OutputType([System.String])] | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter()] | ||
[System.String[]] | ||
$Name = @('SqlServer', 'SQLPS'), | ||
|
||
[Parameter()] | ||
[System.Management.Automation.SwitchParameter] | ||
$Refresh | ||
) | ||
|
||
if ($Refresh.IsPresent) | ||
{ | ||
# Only run on Windows that has Machine state. | ||
if (-not ($IsLinux -or $IsMacOS)) | ||
{ | ||
<# | ||
After installing SQL Server the current PowerShell session doesn't know | ||
about the new path that was added for the SQLPS module. This reloads | ||
PowerShell session environment variable PSModulePath to make sure it | ||
contains all paths. | ||
#> | ||
|
||
<# | ||
TODO: This should be replaced by Get-PSModulePath that is in | ||
PR https://github.com/dsccommunity/DscResource.Common/pull/104. | ||
#> | ||
|
||
<# | ||
Get the environment variables from all targets session, user and machine. | ||
Casts the value to System.String to convert $null values to empty string. | ||
#> | ||
$modulePathSession = [System.String] [System.Environment]::GetEnvironmentVariable('PSModulePath') | ||
$modulePathUser = [System.String] [System.Environment]::GetEnvironmentVariable('PSModulePath', 'User') | ||
$modulePathMachine = [System.String] [System.Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') | ||
|
||
$modulePath = $modulePathSession, $modulePathUser, $modulePathMachine -join ';' | ||
|
||
$modulePathArray = $modulePath -split ';' | | ||
Where-Object -FilterScript { | ||
-not [System.String]::IsNullOrEmpty($_) | ||
} | | ||
Sort-Object -Unique | ||
|
||
$modulePath = $modulePathArray -join ';' | ||
|
||
Set-PSModulePath -Path $modulePath | ||
} | ||
} | ||
|
||
$availableModuleName = $null | ||
|
||
$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 | ||
} | ||
} | ||
} | ||
) | ||
|
||
foreach ($preferredModuleName in $Name) | ||
{ | ||
$preferredModule = $availableModule | | ||
Where-Object -Property 'Name' -EQ -Value $preferredModuleName | ||
|
||
if ($preferredModule) | ||
{ | ||
if ($preferredModule.Name -eq 'SQLPS') | ||
{ | ||
# Get the latest version if available. | ||
$preferredModule = $preferredModule | | ||
Sort-Object -Property 'Version' -Descending | | ||
Select-Object -First 1 | ||
|
||
<# | ||
For SQLPS the path to the module need to be returned as the | ||
module name to be absolutely sure the latest version is used. | ||
#> | ||
$availableModuleName = Split-Path -Path $preferredModule.Path -Parent | ||
} | ||
else | ||
{ | ||
$availableModuleName = ($preferredModule | Select-Object -First 1).Name | ||
} | ||
|
||
Write-Verbose -Message ($script:localizedData.PreferredModule_ModuleFound -f $availableModuleName) | ||
|
||
break | ||
} | ||
} | ||
|
||
if (-not $availableModuleName) | ||
{ | ||
$errorMessage = $script:localizedData.PreferredModule_ModuleNotFound | ||
|
||
# cSpell: disable-next | ||
Write-Error -Message $errorMessage -Category 'ObjectNotFound' -ErrorId 'GSDPM0001' -TargetObject ($Name -join ', ') | ||
} | ||
|
||
return $availableModuleName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.