Functions/Install-PsModuleFast.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<#
.SYNOPSIS
   Installs and Imports a module
.DESCRIPTION
    Encapsulates the logic to ensure the required version is installed and loaded into the session.
    If not installed the module is downloaded to the CurrentUser scope.
    If a version that isn't compatible is loaded it will be removed
    If the version required isn't loaded it will be imported
    You can specify, ```-MinimumVersion```, ```-MaximumVersion``` in which case an appropriate version is loaded if installed. It will not go
    and install a later version even if one exists in the repository unless `-latest` is used
    If required version is specified only that version will be installed and imported .
.EXAMPLE
    none
.OUTPUTS
    none
.NOTES
    none
#>

Function Install-PsModuleFast {
    [CmdletBinding()]
    param([string]
        #Name of the module to install
          $module
        , #Minimum version to install and import
         [version] $version
         #Maximumversion to install and import
        , [version] $MaxVersion
        #The version that has to be found to install. Must be 3 part version number
        , [string] $RequiredVersion
        #Allows for pre release versions to be installed
        , [switch] $AllowPrerelease
        #Repository to look for modules
        , [string] $Repository
        #Will find the latest version within the Min and Max specified and install that
        ,[switch]$Latest
        ,[string]$Scope="CurrentUser")

    if ($PSBoundParameters.ContainsKey("RequiredVersion") -and $requiredVersion -notmatch  "^\d*\.\d*\.\d*") { throw "Required version has to be a 3 part semver version" }

    Write-Verbose "Loading module $Module"

    Repair-PSModulePath;
    
    if ($Latest){ 
        $FindModuleArgs = @{Name=$Module}
        if ($version){$FindModuleArgs.Add("MinimumVersion",$version)}
        if ($MaxVersion){$FindModuleArgs.Add("MaximumVersion",$MaxVersion)}
        if ( $Repository){ $FindModuleArgs.Add("Repository",$Repository)}
            Write-Verbose "Finding Latest version for module"
            $RequiredVersion = (Find-Module @FindModuleArgs).Version
            Write-Verbose "Latest version found $RequiredVersion "
        }

    $InstallmoduleArgs = @{}
    if ($Repository) { $InstallmoduleArgs.Add("Repository", $Repository) }

    $VersionArgs = @{}
    if ($RequiredVersion) { 
        $versionparts = split-version $RequiredVersion
        Write-Verbose "Only using Required Version"
        $VersionArgs.Add("RequiredVersion", $RequiredVersion) 
    # $VersionArgs.Add("prerelease", $versionparts.Prerelease)
    }
    else{
        if ($Version) { $VersionArgs.Add("MinimumVersion", $Version) }
        if ($MaxVersion) { $VersionArgs.Add("MaximumVersion", $MaxVersion) }
    }
    
    if (Test-ShouldInstallModule -Module $Module @VersionArgs) {
        Write-Host " Installing module $module" -NoNewline
        install-module $module -force -AllowClobber -Scope $Scope -SkipPublisherCheck @InstallmoduleArgs @VersionArgs -AllowPrerelease:$AllowPrerelease
        Write-Host " - installed $((get-module $module -ListAvailable).Version)"
    }

    if (Test-ShouldImportModule -module $module @VersionArgs) {
        Write-Host " importing module $module " -NoNewline
     
        remove-moduleWithOutError $module

        #import module doesn't understand prerelease
        if ($RequiredVersion) { 
            $versionparts = split-version $RequiredVersion
            Write-Verbose "Only using Required Version"
            $VersionArgs.RequiredVersion=$versionparts.Version 
        }
        import-module $module -force  @VersionArgs -Global -Verbose:$VerbosePreference
        Write-Host " - imported $((get-module $module).Version)"
    }
    
    Write-Host ("using module {0:-20} - {1}" -f $module, $($(Get-Module $module).version))
}
function remove-moduleWithOutError{
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
    param($Module)

    get-Module $module | ForEach-Object{
        Write-Host " - removing $($_.version)" -NoNewline
        remove-module -Name $_.name -Verbose
    }
}

function split-version{
    param ($version)
    $versionparts = $version | Select-String "^(\d*\.\d*\.\d*)-?(.*)"
    write-Verbose "splitting out $version "
    write-Verbose ($versionparts | Format-List | out-string)

    return @{
        version=$versionparts.Matches.Groups[1].value;
        prerelease=$versionparts.Matches.Groups[2].value
    }
}

function Test-ShouldInstallModule() {
    [CmdletBinding()]
    [OutputType([Boolean])]
    param($module
        , [string]$RequiredVersion
        , [version]$MaximumVersion
        , [version]$MinimumVersion)
        
    $modulesInstalled = get-module $module -ListAvailable

    return Test-ShouldGetVersion -modules $modulesInstalled -RequiredVersion $RequiredVersion -MinimumVersion $MinimumVersion -MaximumVersion $MaximumVersion

}
function Test-ShouldImportModule() {
    [CmdletBinding()]
    [OutputType([Boolean])]
    param($module
        , [string]$RequiredVersion
        , [version]$MaximumVersion
        , [version]$MinimumVersion)
        
    $modulesLoaded = get-module $module 

    return Test-ShouldGetVersion -modules $modulesLoaded -RequiredVersion $RequiredVersion -MinimumVersion $MinimumVersion -MaximumVersion $MaximumVersion

}

function Test-ShouldGetVersion() {
    [CmdletBinding()]
    [OutputType([Boolean])]
    param($modules
         ,[string]$RequiredVersion
         ,[version]$MaximumVersion
         ,[version]$MinimumVersion)

    if ($RequiredVersion -ne "" ){
        write-Verbose "Required version"
        if ($RequiredVersion -match "^\d*\.\d*\.\d*$"){
            if ( -not ($modules.Version -eq $RequiredVersion)) {
                write-Verbose "Required version not found so have to install"
                return $true
            }
        }    
        else {
            $versionparts = split-version $requiredVersion
            if (($modules | where-object {$_.Version -eq $versionparts.version -and $_.privateData.psdata.prerelease -eq $versionparts.prerelease}).Count -eq 0){
                write-Verbose "Required version ($version) for prerelease ($prerelease) not found so have to install"
                return $true
            }

        }
    }
    if ($null -ne $MinimumVersion -and -not ($modules.Version -ge $MinimumVersion)) {
        write-Verbose "Minimum version not found so have to install"
        return $true
    }
    if ($null -ne $MaximumVersion -and -not ($modules.Version -le $MaximumVersion)) {
        write-Verbose "Maximum version modules found so have to install"
        return $true
    }
    if (($null -ne $MinimumVersion) -and ($null -ne $MaximumVersion) ) {
        Write-Verbose "Min and Max"
        if (-not ($modules | Where-Object { $_.Version -ge $MinimumVersion -and $_.Version -le $MaximumVersion })) {
            write-Verbose "Minimum and Maximum version modules found so have to install"
            return $true
        }
    }
    if ($null -eq $modules ) {
        write-Verbose "No modules found so have to install"
        return $true
    }
    write-Verbose "All tests passed don't install - modules installed are $($modules.Version)"
    return $false;

}


# Function Install-PsModuleFast {
# [CmdletBinding()]
# param([string] $module
# , [version] $version
# , [parameter(Mandatory=$false)][string] $path)
#
# Repair-PSModulePath;
#
# Write-Verbose "Loading module $Module"
# if (-not (get-module $(join-path $path $module) -ListAvailable | Where-object Version -ge $version)) {
# Write-Output " Installing module $module"
# if ($null -eq $version) {
# install-module $module -path $path -force -AllowClobber -Scope CurrentUser -SkipPublisherCheck
# }
# else {
# install-module $module -path $path -force -AllowClobber -Scope CurrentUser -SkipPublisherCheck -MinimumVersion $version
# }
# }
#
# if (-not (get-module $module | Where-object Version -GE $version)) {
# Write-Output " importing module $module $version"
# if ($null -eq $version ) {
# import-module $module -force
# }
# else {
# import-module $module -force -MinimumVersion $version
# }
# }
# }

function Repair-PSModulePath {

    if ($PSVersionTable.PsEdition -eq "Core") {
        $mydocsPath = join-path ([System.Environment]::GetFolderPath("MyDocuments")) "PowerShell/Modules"
    }
    else {
        $mydocsPath = join-path ([System.Environment]::GetFolderPath("MyDocuments")) "WindowsPowerShell/Modules"
    }

    If ("$($env:PSModulePath)".Split([IO.Path]::PathSeparator) -notcontains $mydocsPath) {
        Write-Verbose "Adding LocalModule folder to PSModulePath"
        $env:PSModulePath = "$mydocsPath$([IO.Path]::PathSeparator)$($env:PSModulePath)"
    }
}

# $PSModules = @{Module = "Pester"; Version = "4.5" }, `
# @{Module = "Microsoft.PowerApps.PowerShell" }, `
# @{Module = "Microsoft.PowerApps.Administration.PowerShell" }, `
# @{Module = "VSSetup" }
# foreach ($Ps in $PSModules) {
#
# Install-PsModuleFast @PS
# }