Functions/Import-ExternalModule.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
<#
.SYNOPSIS
    This function installs and imports an external module.
.DESCRIPTION
    This function installs and imports an external module.
    The use of this module is preferred over Install-Module and Import-Module when a specific version
    of a module is required as using Install-Module with both the -RequiredVersion and -Force parameters
    results in a re-download and re-installation every time, even if the required version is already
    installed, which results in unnecessary slowness.
    Import-ExternalModule removes this issue by skipping the installation if the required version
    is already installed.
#>

function Import-ExternalModule {
    [CmdletBinding(PositionalBinding=$true)]
    param (
        # The name of the module to import.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$moduleName,

        # The required version of the module to import.
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [String]$requiredVersion,

        # Select whether to suppress information messages.
        [Parameter(Mandatory=$false)]
        [Switch]$quiet
    )

    # Install and import the module
    try {
        # Install the module
        if ($requiredVersion) {
            # Retrieve installed versions of the module
            $installedVersions = (Get-ChildItem -Path "$($env:USERPROFILE)\Documents\WindowsPowerShell\Modules\$($moduleName)" `
                -ErrorAction SilentlyContinue).Name

            # Check if the required version has already been installed
            if ($requiredVersion -notIn $installedVersions) {
                Install-Module $moduleName -RequiredVersion $requiredVersion -Force -AllowClobber -Scope CurrentUser
                if (!$quiet) {
                    Write-Information "$($moduleName) version $($requiredVersion) has been installed."
                }
            }
            else {
                if (!$quiet) {
                    Write-Information "$($moduleName) version $($requiredVersion) is already installed."
                }
            }
        }
        else {
            # Retrieve installed versions of the module
            $installedVersions = (Get-ChildItem -Path "$($env:USERPROFILE)\Documents\WindowsPowerShell\Modules\$($moduleName)" `
                -ErrorAction SilentlyContinue).Name

            # Check if any version of the module has already been installed
            if ($installedVersions) {
                if (!$quiet) {
                    Write-Information "$($moduleName) is already installed."
                }
            }
            else {
                Install-Module $moduleName -Force -AllowClobber -Scope CurrentUser
                if (!$quiet) {
                    Write-Information "$($moduleName) has been installed."
                }
            }
        }

        # Import the module
        $userModulesPath = "$($env:USERPROFILE)\Documents\WindowsPowerShell\Modules"
        if ($requiredVersion) {
            Import-Module -Name "$($userModulesPath)\$($moduleName)" -RequiredVersion $requiredVersion -Force -Global
            if (!$quiet) {
                Write-Information "Imported $($moduleName) version $($requiredVersion)."
            }
        }
        else {
            Import-Module -Name "$($userModulesPath)\$($moduleName)" -Force -Global
            if (!$quiet) {
                Write-Information "Imported $($moduleName)."
            }
        }
    }
    catch {
        Write-Error "Exception occurred on line $($_.InvocationInfo.ScriptLineNumber): `r`n$($_.Exception.Message)"
    }
    finally {
        $InformationPreference = $previousInformationPreference
    }
}