Private/Install-SpecModule.ps1

function Install-SpecModule {
<#
    .SYNOPSIS
        Installs a specified PowerShell module from the PowerShell Gallery.
 
    .DESCRIPTION
        The Install-SpecModule function installs a specified PowerShell module from the PowerShell Gallery using the
        Install-Module cmdlet from the PowerShellGet module. It allows installation for all users or the current user,
        and it can install a specific version of the module if required. The function can also accept the license agreement
        automatically using the -AcceptLicense switch.
 
    .PARAMETER Module
        Specifies the name of the PowerShell module to install from the PowerShell Gallery.
 
    .PARAMETER Scope
        Specifies the scope in which to install the PowerShell module. Use "AllUsers" to install it for all users
        or "CurrentUser" to install it only for the current user.
 
    .VALIDATION
        Valid values for the Scope parameter are "AllUsers" and "CurrentUser".
 
    .PARAMETER RequiredVersion
        Specifies the version of the PowerShell module to install. If not specified, the latest version will be installed.
 
    .PARAMETER AcceptLicense
        Indicates whether to accept the license agreement for the module automatically. If this switch is used, the function
        will not prompt the user for acceptance.
 
    .EXAMPLE
        Install-SpecModule -Module "ExampleModule" -Scope AllUsers -RequiredVersion "1.2.0" -AcceptLicense
        Installs version 1.2.0 of "ExampleModule" for all users automatically accepting the license.
 
    .EXAMPLE
        Install-SpecModule -Module "AnotherModule" -Scope CurrentUser
        Installs the latest version of "AnotherModule" for the current user.
 
    .EXAMPLE
        Install-SpecModule -Module "NonExistentModule" -Scope AllUsers
        Displays a warning message stating that the module "NonExistentModule" could not be found in the PowerShell Gallery.
 
    .NOTES
        Author: owen.heaume
        Version: 1.0 - initial function
        - If the installation is successful, the function returns "True"; otherwise, it returns an error code:
            - 609: An error occured when attempting to install the module using the -AcceptLicense switch.
            - 610: An error occured when attempting to install the module without the -AcceptLicense switch.
    #>

    [cmdletbinding()]
    param (
        [parameter (Mandatory = $true)]
        [string]$Module,

        [parameter (Mandatory = $true)]
        [ValidateSet("AllUsers", "CurrentUser")]
        [String]$Scope,

        [parameter (Mandatory = $false)]
        [string]$RequiredVersion = "",

        [Switch]
        $AcceptLicense

    )

    $params = @{
        name               = $Module
        Scope              = $Scope
        Force              = $true
        ErrorVariable      = 'x'
        ErrorAction        = 'Stop'
        Confirm            = $false
        Verbose            = $false
        AllowClobber       = $true
        SkipPublisherCheck = $true
        MaximumVersion     = $RequiredVersion
    }

    if ($AcceptLicense) {
        write-verbose "Installing module $module with -AcceptLicense parameter"
        try {
            Install-Module @params -AcceptLicense
            return $true
        } catch {
            Write-warning "An error occured when attempting to install the module: $Module"
            Write-Warning "The error was: $x"
            return 609
        }
    } else {
        try {
            write-verbose "Installing module $module"
            Install-Module @params
            return $true
        } catch {
            Write-warning "An error occured when attempting to install the module: $Module"
            Write-Warning "The error was: $x"
            return 610
        }
    }
}