-
Notifications
You must be signed in to change notification settings - Fork 225
/
Import-SqlDscPreferredModule.ps1
152 lines (122 loc) · 4.77 KB
/
Import-SqlDscPreferredModule.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<#
.SYNOPSIS
Imports a (preferred) module in a standardized way.
.DESCRIPTION
Imports a (preferred) module in a standardized way. If the parameter `Name`
is not specified the command will imports the default module SqlServer
if it exist, otherwise SQLPS.
If the environment variable `SMODefaultModuleName` is set to a module name
that name will be used as the preferred module name instead of the default
module 'SqlServer'.
The module is always imported globally.
.PARAMETER Name
Specifies the name of a preferred module.
.PARAMETER Force
Forces the removal of the previous module, to load the same or newer version
fresh. This is meant to make sure the newest version is used, with the latest
assemblies.
.EXAMPLE
Import-SqlDscPreferredModule
Imports the default preferred module (SqlServer) if it exist, otherwise
it will try to import the module SQLPS.
.EXAMPLE
Import-SqlDscPreferredModule -Force
Will forcibly import the default preferred module if it exist, otherwise
it will try to import the module SQLPS. Prior to importing it will remove
an already loaded module.
.EXAMPLE
Import-SqlDscPreferredModule -Name 'OtherSqlModule'
Imports the specified preferred module OtherSqlModule if it exist, otherwise
it will try to import the module SQLPS.
#>
function Import-SqlDscPreferredModule
{
[CmdletBinding()]
param
(
[Parameter()]
[Alias('PreferredModule')]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,
[Parameter()]
[System.Management.Automation.SwitchParameter]
$Force
)
$getSqlDscPreferredModuleParameters = @{
Refresh = $true
}
if ($PSBoundParameters.ContainsKey('Name'))
{
$getSqlDscPreferredModuleParameters.Name = @($Name, 'SQLPS')
}
$availableModuleName = Get-SqlDscPreferredModule @getSqlDscPreferredModuleParameters
if ($Force.IsPresent)
{
Write-Verbose -Message $script:localizedData.PreferredModule_ForceRemoval
$removeModule = @()
if ($PSBoundParameters.ContainsKey('Name'))
{
$removeModule += $Name
}
# Available module could be
if ($availableModuleName)
{
$removeModule += $availableModuleName
}
if ($removeModule -contains 'SQLPS')
{
$removeModule += 'SQLASCmdlets' # cSpell: disable-line
}
Remove-Module -Name $removeModule -Force -ErrorAction 'SilentlyContinue'
}
if ($availableModuleName)
{
if (-not $Force.IsPresent)
{
# Check if the preferred module is already loaded into the session.
$loadedModuleName = (Get-Module -Name $availableModuleName | Select-Object -First 1).Name
if ($loadedModuleName)
{
Write-Verbose -Message ($script:localizedData.PreferredModule_AlreadyImported -f $loadedModuleName)
return
}
}
try
{
Write-Debug -Message ($script:localizedData.PreferredModule_PushingLocation)
Push-Location
<#
SQLPS has unapproved verbs, disable checking to ignore Warnings.
Suppressing verbose so all cmdlet is not listed.
#>
$importedModule = Import-Module -Name $availableModuleName -DisableNameChecking -Verbose:$false -Force:$Force -Global -PassThru -ErrorAction 'Stop'
<#
SQLPS returns two entries, one with module type 'Script' and another with module type 'Manifest'.
Only return the object with module type 'Manifest'.
SqlServer only returns one object (of module type 'Script'), so no need to do anything for SqlServer module.
#>
if ($availableModuleName -eq 'SQLPS')
{
$importedModule = $importedModule | Where-Object -Property 'ModuleType' -EQ -Value 'Manifest'
}
Write-Verbose -Message ($script:localizedData.PreferredModule_ImportedModule -f $importedModule.Name, $importedModule.Version, $importedModule.Path)
}
finally
{
Write-Debug -Message ($script:localizedData.PreferredModule_PoppingLocation)
Pop-Location
}
}
else
{
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
($script:localizedData.PreferredModule_FailedFinding),
'ISDPM0001', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
'PreferredModule'
)
)
}
}