Public/Import-SpecPowerShellModule.ps1

Function Import-SpecPowerShellModule {
    <#
    .SYNOPSIS
        Imports a specified PowerShell module into the current session.
 
    .DESCRIPTION
        The Import-SpecPowerShellModule function allows you to import a specified PowerShell module into the current
        PowerShell session. If the module is already imported, the function will unload it first and then re-import it.
        You can also specify a required version of the module to import.
 
    .PARAMETER Module
        Specifies the name of the PowerShell module to import. This parameter supports pipeline input.
 
    .PARAMETER RequiredVersion
        Specifies the required version of the PowerShell module to import. If this parameter is provided, the function
        will attempt to import the specified version of the module. If not provided, it will import the latest available version.
 
    .EXAMPLE
        "ExampleModule" | Import-SpecPowerShellModule
        Imports the "ExampleModule" PowerShell module into the current session.
 
    .EXAMPLE
        Import-SpecPowerShellModule -Module "AnotherModule" -RequiredVersion "1.0.0"
        Imports version 1.0.0 of the "AnotherModule" PowerShell module.
 
    .EXAMPLE
        "YetAnotherModule" | Import-SpecPowerShellModule -RequiredVersion "2.1.0"
        Imports version 2.1.0 of the "YetAnotherModule" PowerShell module using pipeline input.
    .EXAMPLE
        $Modules = @(
            @{
                moduleName = 'SpecBaseModule'
                requiredVersion = "1.0.2"
            },
            @{
                moduleName = 'Az.Accounts'
                requiredVersion = '2.10.2'
            }
        )
 
        $modules | % { Import-SpecPowerShellModule -Module $_['moduleName'] -RequiredVersion $_['requiredVersion'] -Verbose }
        Imports the specified versions of the modules "SpecBaseModule" and "Az.Accounts" into the current session.
    .EXAMPLE
        $Modules = @(
            @{
                moduleName = 'SpecBaseModule'
                requiredVersion = ""
            },
            @{
                moduleName = 'Az.Accounts'
                requiredVersion = '2.10.2'
            }
        )
 
        $modules | % { Import-SpecPowerShellModule -Module $_['moduleName'] -RequiredVersion $_['requiredVersion'] -Verbose }
        Imports the latest version of the module "SpecBaseModule" and imports version 2.10.2 of "Az.Accounts" into the current session.
 
    .NOTES
        Author: owen.heaume
        Version: 1.0 - initial function
        - If an error occurs during the import, the function writes an error message and exits with code 501.
    #>


    [cmdletbinding()]

    param (
        [parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true)]
        [string]$Module,

        [string]$RequiredVersion
    )

    write-host "`n"
    $importedModules = Get-Module -Name $module -ErrorAction SilentlyContinue
    $version = $importedModules.Version


    if ($importedModules) {
        Write-Verbose "The module '$module' is already imported."
        $imported = $true
    } else {
        Write-verbose "The module '$module' is not already imported."
        $imported = $false
    }

    # Attempt to import module
    try {
        # Unload any existing module if loaded already
        if ($imported) {
            write-verbose "Unloading $Module..."
            Remove-Module $Module -Verbose:$false -Force -ErrorAction SilentlyContinue
        }

        if ($RequiredVersion -ne "") {
            write-verbose "Importing module version: $RequiredVersion..."
            Import-Module $Module -RequiredVersion $RequiredVersion -Verbose:$false
        } else {
            write-verbose "Importing module version: $version"
            Import-Module $Module -Verbose:$false

        }
    } catch [System.Exception] {
        Write-Error "ERROR - unable to import the $Module module"
        Exit 501
    }

    Write-Verbose "Import of Module was successful"

}