Noveris.ModuleMgmt.psm1
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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
<# #> ######## # Global settings $InformationPreference = "Continue" $ErrorActionPreference = "Stop" Set-StrictMode -Version 2 <# #> Function Use-PowerShellGallery { [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory=$false)] [ValidateSet("AllUsers", "CurrentUser")] [string]$Scope = "CurrentUser" ) process { Write-Verbose "Installing Nuget package provider" if ($PSCmdlet.ShouldProcess("Nuget Provider", "Update")) { Write-Verbose "Attempting nuget provider update" try { # Set TLS support to 1.1 and 1.2 explicitly [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11 Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false -Scope $Scope } catch { Write-Warning "Couldn't install nuget package provider" } } Write-Verbose "Trusting PSGallery" if ($PSCmdlet.ShouldProcess("PSGallery Repository", "Trust")) { try { Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted } catch { Write-Verbose ("Set-PSRepository for PSGallery failed: " + $_) } } } } <# #> Function Install-PSModuleFromManifest { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')] [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$ManifestPath, [Parameter(Mandatory=$false)] [ValidateSet("AllUsers", "CurrentUser")] [string]$Scope = "CurrentUser", [Parameter(Mandatory=$false)] [switch]$Force = $false ) process { Write-Verbose "Reading content from manifest: $ManifestPath" $content = Get-Content -Encoding UTF8 $ManifestPath | Out-String # Convert to a script block and execute Write-Verbose "Converting to manifest object" $block = [ScriptBlock]::Create($content) $meta = & $block | Select-Object -First 1 # Check that we have received a hashtable from the manifest file if ($null -eq $meta -or $meta.GetType().FullName -ne "System.Collections.Hashtable") { Write-Error "Invalid type returned from manifest file" return } # Check if we have any required modules to install if ($meta.Keys -notcontains "RequiredModules") { Write-Verbose "No required modules to install or section missing" return } $meta["RequiredModules"] | ForEach-Object { $module = $_ # Attempt best effort to continue if there is an invalid entry if ($null -eq $module -or $module.GetType().FullName -ne "System.Collections.Hashtable") { Write-Warning "Missing or invalid type in RequiredModules" return } # ModuleName is mandatory in the entry if ($module.Keys -notcontains "ModuleName") { Write-Warning "Missing module name in RequiredModules entry" return } # Translate keys from the RequiredModules entry to parameters for # Install-Module $installParams = @{ Scope = $Scope Force = $Force } ("ModuleName", "Name"), ("RequiredVersion", "RequiredVersion"), ("MaximumVersion", "MaximumVersion"), ("ModuleVersion", "MinimumVersion") | ForEach-Object { if ($module.Keys -contains $_[0]) { $installParams[$_[1]] = $module[$_[0]] } } # Install the module Write-Verbose ("Installing module: {0}" -f $installParams["Name"]) if ($PSCmdlet.ShouldProcess($installParams["Name"], "Install")) { Install-Module @installParams } } } } <# #> Function Select-ModuleVersionMatches { [OutputType("System.String")] [CmdletBinding()] param( [Parameter(Mandatory=$true,ValueFromPipeline)] [ValidateNotNullOrEmpty()] [string]$Version, [Parameter(Mandatory=$false)] [ValidateNotNull()] [int]$Major = -1, [Parameter(Mandatory=$false)] [ValidateNotNull()] [int]$Minor = -1, [Parameter(Mandatory=$false)] [ValidateNotNull()] [int]$Patch = -1, [Parameter(Mandatory=$false)] [switch]$StrictParse = $false ) process { try { Write-Verbose "Parsing version $Version" $parsed = [Version]::Parse($Version) if ($Major -ge 0 -and $parsed.Major -ne $Major) { return } if ($Minor -ge 0 -and $parsed.Minor -ne $Minor) { return } if ($Patch -ge 0 -and $parsed.Build -ne $Patch) { return } $Version } catch { if ($StrictParse) { Write-Error "Failed to parse version string: $Version" } else { Write-Warning "Failed to parse version string: $Version" } } } } <# #> Function Install-PSModuleWithSpec { [CmdletBinding()] param( [Parameter(mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Name, [Parameter(Mandatory=$false)] [ValidateNotNull()] [int]$Major = -1, [Parameter(Mandatory=$false)] [ValidateNotNull()] [int]$Minor = -1, [Parameter(Mandatory=$false)] [ValidateNotNull()] [int]$Patch = -1, [Parameter(mandatory=$false)] [switch]$Offline = $false, [Parameter(Mandatory=$false)] [ValidateSet("CurrentUser", "AllUsers")] [string]$Scope = "CurrentUser" ) process { # If not offline, install the latest version that matches the spec, if it doesn't exist locally if (!$Offline) { $target = $null try { # Get the modules available online and filter by version spec $target = Find-Module -AllVersions -Name $Name -EA Stop | ForEach-Object { $_.Version.ToString() } | Select-ModuleVersionMatches -Major $Major -Minor $Minor -Patch $Patch | Select-Object -First 1 Write-Verbose "Find module result: $target" } catch { Write-Warning "Failed to get online module info for $Name" } # Get the local modules $installed = Get-Module -ListAvailable -Name $Name | ForEach-Object { $_.Version.ToString() } Write-Verbose ("Found local modules: " + ($installed -join ",")) # If we found a match online, check if it is installed locally if (![string]::IsNullOrEmpty($target) -and ($null -eq $installed -or $installed -notcontains $target)) { Write-Verbose "Installing module $Name version $target" Install-Module -Name $Name -RequiredVersion $target -Force -SkipPublisherCheck -Scope $Scope } } # Get the local modules and filter by spec $target = Get-Module -ListAvailable -Name $Name | ForEach-Object { $_.Version.ToString() } | Select-ModuleVersionMatches -Major $Major -Minor $Minor -Patch $Patch | Select-Object -First 1 if ($null -eq $target) { Write-Error "Could not find suitable installed version for $Name" } else { # Return matching target version Write-Verbose "Found local module to import: $target" $target } } } |