-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-OnlineModules.ps1
More file actions
148 lines (136 loc) · 7.09 KB
/
Get-OnlineModules.ps1
File metadata and controls
148 lines (136 loc) · 7.09 KB
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
<#
.SYNOPSIS
Saves PowerShell Modules for import to diconnect networks
.DESCRIPTION
Run this script on a internet connected system
this script will download latest nuget assembly with packagemanagement modules
plus any additional module found. Required for disconnected system
.NOTES
https://docs.microsoft.com/en-us/powershell/gallery/psget/repository/bootstrapping_nuget_proivder_and_exe
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$false,Position=0,HelpMessage='Specify modules to download. for multiple, separate by commas')]
[string[]]$OnlineModules,
[Parameter(Mandatory=$false,Position=1,HelpMessage='Remove older modules if found')]
[switch]$RemoveOld = $true,
[Parameter(Mandatory=$false,Position=1,HelpMessage='Install modules on online system as well')]
[switch]$Install = $true,
[Parameter(Mandatory=$false,Position=1,HelpMessage='Force modules to re-import and install')]
[switch]$ForceInstall = $false,
[Parameter(Mandatory=$false,Position=1,HelpMessage='Re-Download modules if exist')]
[switch]$Refresh = $false
)
##*===============================================
##* VARIABLE DECLARATION
##*===============================================
## Variables: Script Name and Script Paths
[string]$scriptPath = $MyInvocation.MyCommand.Definition
[string]$scriptName = [IO.Path]::GetFileNameWithoutExtension($scriptPath)
[string]$scriptFileName = Split-Path -Path $scriptPath -Leaf
[string]$scriptRoot = Split-Path -Path $scriptPath -Parent
[string]$invokingScript = (Get-Variable -Name 'MyInvocation').Value.ScriptName
#Get required folder and File paths
[string]$DownloadedModulesPath = Join-Path -Path $scriptRoot -ChildPath 'DownloadedModules'
If(!$OnlineModules){
$OnlineModules = "PowerShellGet","VMware.PowerCLI","PowervRA","PowervRO"
}
##*===============================================
##*==========================================================================
##* FUNCTION
##*==========================================================================
Function Output-Prefix{
[string]$timezone = [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])\w+\s*', '$1')
[string]$date = Get-Date -Format ("ddd MMM dd hh:mm:ss {0} yyyy" -f $timezone)
return $date
}
$Prefix = Output-Prefix
#See if system is conencted to the internet
$internetConnected = Test-NetConnection www.powershellgallery.com -CommonTCPPort HTTP -InformationLevel Quiet -WarningAction SilentlyContinue
If($internetConnected)
{
$Nuget = Install-PackageProvider Nuget –force –verbose
$NuGetAssemblyVersion = $($Nuget).version
Write-Host "INSTALLED: Nuget [$NuGetAssemblyVersion] was installed" -ForegroundColor Green
#Copy Nuget prereqs
$NuGetAssemblyDestPath = Get-ChildItem "$DownloadedModulesPath\\nuget" -Filter *.dll -Recurse
$NuGetAssemblySourcePath = "$env:ProgramFiles\PackageManagement\ProviderAssemblies\nuget"
If ($NuGetAssemblyDestPath.FullName)
{
If($Refresh){
Write-Host "BACKUP: Copying nuget Assembly [$NuGetAssemblyVersion] from $NuGetAssemblySourcePath" -ForegroundColor Gray
Copy-Item "$NuGetAssemblySourcePath\$NuGetAssemblyVersion\Microsoft.PackageManagement.NuGetProvider.dll" $NuGetAssemblyDestPath.FullName -Force -ErrorAction SilentlyContinue
}
Else{
Write-Host "FOUND: Nuget [$NuGetAssemblyVersion] already copied" -ForegroundColor Green
}
}
Else{
Write-Host "BACKUP: Copying nuget Assembly [$NuGetAssemblyVersion] from $NuGetAssemblySourcePath" -ForegroundColor Gray
New-Item "$DownloadedModulesPath\nuget\$NuGetAssemblyVersion" -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
Copy-Item "$NuGetAssemblySourcePath\$NuGetAssemblyVersion\Microsoft.PackageManagement.NuGetProvider.dll" "$DownloadedModulesPath\nuget\$NuGetAssemblyVersion" -ErrorAction SilentlyContinue
}
#loop through each module
Foreach ($Module in $OnlineModules){
#get the module if found online
$ModuleFound = Find-Module $Module
If($ModuleFound)
{
[string]$ModuleVersion = $ModuleFound.Version
[string]$ModuleName = $ModuleFound.Name
#If specified, remove older modules in DownloadedModule directory if found
If($RemoveOld)
{
$LikeModulesExist = Get-ChildItem $DownloadedModulesPath -Directory | Where-Object {$_.FullName -match "$ModuleName" -and $_.FullName -notmatch "$ModuleName-$ModuleVersion"} | foreach {
$_ | Remove-Item -Force -Recurse
Write-host "REMOVED: $($_.FullName)" -ForegroundColor DarkYellow
}
}
#Check to see it module is already downloaded
If(Test-Path "$DownloadedModulesPath\$ModuleName-$ModuleVersion")
{
#If specified, Re-Download modules
If($Refresh)
{
Write-Host "BACKUP: $ModuleName [$ModuleVersion] found but will be re-downloaded..." -ForegroundColor Yellow
Save-Module -Name $ModuleName -Path $DownloadedModulesPath\$ModuleName-$ModuleVersion -Force
}
Else{
Write-Host "FOUND: $ModuleName [$ModuleVersion] already downloaded" -ForegroundColor Gray
}
}
Else{
Write-Host "BACKUP: $ModuleName [$ModuleVersion] not found, downloading for offline install" -ForegroundColor Gray
New-Item "$DownloadedModulesPath\$ModuleName-$ModuleVersion" -ItemType Directory | Out-Null
Save-Module -Name $ModuleName -Path $DownloadedModulesPath\$ModuleName-$ModuleVersion
}
#If specified, Install modules on local system as well
If($Install)
{
If([string](Get-Module $Module).Version -ne $ModuleVersion -or $ForceInstall){
Try{
Write-Host "INSTALL: $Module [$ModuleVersion] will be installed locally as well, please wait..." -ForegroundColor Gray
Install-Module $Module -AllowClobber -SkipPublisherCheck -Force
Import-Module $Module
}
Catch{
Write-Output ("[{0}][{1}] Failed to install and import $Module" -f " $Module",$Prefix)
Write-Output $_.Exception | format-list -force
Exit $_.ExitCode
}
}
Else{
Write-Host "INSTALL: $Module [$ModuleVersion] is already installed, skipping install..." -ForegroundColor Gray
}
}
}
Else{
Write-Host "WARNING: $Module was not found online" -ForegroundColor Yellow
}
Write-Host "COMPLETED: Done working with module: $Module" -ForegroundColor Green
} #End Loop
}
Else{
Write-Host "ERROR: Unable to connect to the internet to grab modules" -ForegroundColor Red
throw $_.error
}