-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoad-Module.ps1
More file actions
82 lines (69 loc) · 2.4 KB
/
Load-Module.ps1
File metadata and controls
82 lines (69 loc) · 2.4 KB
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
#Requires -Version 7.0
<#
.SYNOPSIS
Ensures a PowerShell module is available and imported, installing it if needed.
.DESCRIPTION
Resolves a module by name in three steps: if it is already loaded it does
nothing, if it is installed but not loaded it imports it, and if it is neither
it attempts to install it from the PowerShell Gallery (current-user scope) and
then import it. If the module cannot be found in the gallery the script logs
the failure and exits with code 1.
.PARAMETER Module
Name of the module to load. Must be a non-empty string matching a module that
is installed locally or published to the PowerShell Gallery.
.INPUTS
None. This script does not accept pipeline input.
.OUTPUTS
None. Progress and outcome are written to the log stream.
.EXAMPLE
./Load-Module.ps1 -Module 'Az.Accounts'
Imports Az.Accounts, installing it from the PowerShell Gallery first if it is
not already present on the machine.
.NOTES
Author: Sebastian Gräf
Repo: https://github.com/segraef/Scripts
#>
#region Parameters
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Module
)
#endregion
#region Initialisation
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
Import-Module "$PSScriptRoot/Write-Log.psm1" -Force
#endregion
#region Execution
Write-Log "Executing $($MyInvocation.MyCommand.Name)."
try {
if (Get-Module | Where-Object { $_.Name -eq $Module }) {
Write-Log "Module '$Module' is already imported."
}
elseif (Get-Module -ListAvailable | Where-Object { $_.Name -eq $Module }) {
if ($PSCmdlet.ShouldProcess($Module, 'Import module')) {
Import-Module $Module
Write-Log "Imported module '$Module'."
}
}
elseif (Find-Module -Name $Module | Where-Object { $_.Name -eq $Module }) {
if ($PSCmdlet.ShouldProcess($Module, 'Install and import module')) {
Install-Module -Name $Module -Force -Scope CurrentUser
Import-Module $Module
Write-Log "Installed and imported module '$Module'."
}
}
else {
Write-Log "Module '$Module' not imported, not available and not in the online gallery, exiting." -Level Warning
exit 1
}
}
catch {
Write-Log -Message "Failed to load module '$Module'." -ErrorRecord $_
throw
}
Write-Log "Finished executing $($MyInvocation.MyCommand.Name)."
#endregion