Public/Install-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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function Install-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 Repository
        Specifies the friendly name of a repository that has been registered by running Register-PSRepository
    .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)]
        [Version]$MinimumVersion,

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

        [Parameter(Mandatory=$False)]
        [String]$Repository="PSGallery",

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

        [Parameter(Mandatory=$False)]
        [ValidateSet('AllUsers','CurrentUser','VirtualEnv')]
        [String]$Scope="VirtualEnv"
    )

    # Get Dict of parameters
    $ModuleParams = Get-FunctionParameters

    foreach ($ModuleName in $Name) {
        Try {
            if($Scope -eq 'VirtualEnv') {
                if($VirtualenvFolder){
                    # Add Path to install module in Virtualenv Folder
                    $ModuleParams['Path'] = $VirtualenvFolder

                    # Remove Scope To avoid error on Save-Module
                    $ModuleParams.Remove("Scope")

                    # Save Module to Virtualenv
                    PowerShellGet\Save-Module @ModuleParams
                } else {
                    Write-Error "Virtualenv has not been Enabled, please run Enable-VirtualEnv !"
                }
            } else {
                PowerShellGet\Install-Module @ModuleParams
            }
        } Catch {
            Write-Error "Unable to install $ModuleName Module : $($_.Exception.Message)"
        }
    }
}