Public/Import-Module.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
Function Import-Module {
    <#
    .SYNOPSIS
        Warper to install a module on your VirtualEnv
    .PARAMETER Name
        Specifies an array of names of modules to install.
    .PARAMETER RequiredVersion
        Specifies the exact version number of the module to install.
    .PARAMETER Force
        Forces the command to run without asking for user confirmation.
    .PARAMETER Scope
        Specifies the installation scope of the module.
    .EXAMPLE
        Install-Module -Name Pester
        Install-Module -Name Pester -Force
        Install-Module -Name Pester -Scope CurrentUser
    .NOTES
        Version: 1.0.0
        Author: Thomas ILLIET
        Creation Date: 21/07/2018
    #>

    Param(
        [Parameter(Mandatory=$True)]
        [String[]]$Name,

        [Parameter(Mandatory=$False)]
        [Version]$RequiredVersion,

        [Parameter(Mandatory=$False)]
        [Switch]$Force,

        [Parameter(Mandatory=$False)]
        [Switch]$DisableNameChecking,

        [Parameter(Mandatory=$False)]
        [Switch]$PassThru
    )

    # Get Dict of parameters
    $ModuleParams = Get-FunctionParameters

    foreach ($ModuleName in $Name) {
        Try {
            if(Test-Path -Path $ModuleName) {
                Microsoft.PowerShell.Core\Import-Module @ModuleParams -Global
            } else {
                if($VirtualenvFolder){
                    $ModuleParams['Name'] = Get-ModuleManifest -Path $VirtualenvFolder -Name $ModuleName
                    Microsoft.PowerShell.Core\Import-Module @ModuleParams -Global
                } else {
                    Write-Error "Virtualenv has not been Enabled, please run Enable-VirtualEnv !"
                }
            }
        } Catch {
            Write-Error "Unable to import $ModuleName Module : $_"
        }
    }
}