-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInstall-AGMPowerCLI.ps1
More file actions
294 lines (268 loc) · 8.79 KB
/
Install-AGMPowerCLI.ps1
File metadata and controls
294 lines (268 loc) · 8.79 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# Copyright 2022 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
function GetAGMPowerCLIInstall
{
# Returns the known installation locations for the AGMPowerCLI Module
return Get-Module -ListAvailable -Name AGMPowerCLI -ErrorAction SilentlyContinue | Select-Object -Property Name, Version, ModuleBase
}
function GetPSModulePath
{
# Returns all available PowerShell Module paths
# Windows uses semi-colons, Linux and Mac use colons, go figure.
$platform=$PSVersionTable.platform
if ( $platform -match "Unix" )
{
return $env:PSModulePath.Split(':')
}
else
{
$hostVersionInfo = (get-host).Version.Major
if ( $hostVersionInfo -lt "6" )
{
return $env:PSModulePath.Split(';')
}
else
{
return $env:PSModulePath.Split(';') -notmatch "WindowsPowerShell"
}
}
}
function InstallMenu
{
# Creates a menu of available install or upgrade locations for the module
Param(
[Array]$InstallPathList,
[ValidateSet('installation','upgrade or delete')]
[String]$InstallAction
)
$i = 1
foreach ($Location in $InstallPathList)
{
Write-Host -Object "$i`: $Location"
$i++
}
While ($true)
{
[int]$LocationSelection = Read-Host -Prompt "`nPlease select an $InstallAction path"
if ($LocationSelection -lt 1 -or $LocationSelection -gt $InstallPathList.Length)
{
Write-Host -Object "Invalid selection. Please enter a number in range [1-$($InstallPathList.Length)]"
}
else
{
break
}
}
return $InstallPathList[($LocationSelection - 1)]
}
function RemoveModuleContent
{
# Attempts to remove contents from an existing installation
Remove-Item -Path $InstallPath -Recurse -Force -ErrorAction Stop -Confirm:$true
}
function CreateModuleContent
{
$platform=$PSVersionTable.platform
# Attempts to create a new folder and copy over the AGMPowerCLI Module contents
try
{
if ( $platform -notmatch "Unix" )
{
$null = Get-ChildItem -Path $PSScriptRoot\AGMPowerCLI* -Recurse | Unblock-File
}
$null = New-Item -ItemType Directory -Path $InstallPath -Force -ErrorAction Stop
$null = Copy-Item $PSScriptRoot\AGMPowerCLI* $InstallPath -Force -Recurse -ErrorAction Stop
$null = Test-Path -Path $InstallPath -ErrorAction Stop
$commandcheck = get-command -module AGMPowerCLI
if (!($commandcheck))
{
Write-Host -Object "`nInstallation failed."
}
else {
Write-Host -Object "`nInstallation successful."
}
}
catch
{
throw $_
}
}
function ReportAGMPowerCLI
{
# Removes the AGMPowerCLI Module from the active session and displays a list of all current install locations
Remove-Module -Name AGMPowerCLI -ErrorAction SilentlyContinue
GetAGMPowerCLIInstall
}
### Code
$hostVersionInfo = (get-host).Version.Major
if ( $hostVersionInfo -lt "5" )
{
Write-Host "This module only works with PowerShell Version 5 or above. You are running version $hostVersionInfo."
Write-Host "You will need to install PowerShell Version 5 or higher and try again"
break
}
Import-LocalizedData -BaseDirectory $PSScriptRoot\ -FileName AGMPowerCLI.psd1 -BindingVariable ActModuleData
function silentinstall0
{
Write-host 'Detected PowerShell version: ' $hostVersionInfo
Write-host 'Downloaded AGMPowerCLI version:' $ActModuleData.ModuleVersion
$platform=$PSVersionTable.platform
# if we find an install then we upgrade it
[Array]$ActInstall = GetAGMPowerCLIInstall
if ($ActInstall.name.count -gt 1)
{
Write-Host -Object "`nMultipe installations detected. Silent Installation failed."
}
if ($ActInstall.name.count -eq 1)
{
$InstallPath = $ActInstall.ModuleBase
Write-host 'Found AGMPowerCLI version: ' $ActInstall.Version 'in ' $InstallPath
Remove-Item -Path $InstallPath -Recurse -Force -ErrorAction Stop -Confirm:$false
}
else
{
$InstallPathList = GetPSModulePath
$InstallPath = $InstallPathList[0]
if ( $platform -notmatch "Unix" )
{
$InstallPath = $InstallPath + '\AGMPowerCLI\'
}
else {
$InstallPath = $InstallPath + '/AGMPowerCLI/'
}
}
if ( $platform -notmatch "Unix" )
{
$null = Get-ChildItem -Path $PSScriptRoot\AGMPowerCLI* -Recurse | Unblock-File
}
$null = New-Item -ItemType Directory -Path $InstallPath -Force -ErrorAction Stop
$null = Copy-Item $PSScriptRoot\AGMPowerCLI* $InstallPath -Force -Recurse -ErrorAction Stop
$null = Test-Path -Path $InstallPath -ErrorAction Stop
$commandcheck = get-command -module AGMPowerCLI
if (!($commandcheck))
{
Write-Host 'Silent Installation failed.'
}
else {
Write-Host 'Installed AGMPowerCLI version: ' $ActModuleData.ModuleVersion 'in ' $InstallPath
}
exit
}
function silentinstall
{
Write-host 'Detected PowerShell version: ' $hostVersionInfo
Write-host 'Downloaded AGMPowerCLI version:' $ActModuleData.ModuleVersion
$platform=$PSVersionTable.platform
# if we find an install then we upgrade it
[Array]$ActInstall = GetAGMPowerCLIInstall
if ($ActInstall.name.count -gt 1)
{
Write-Host -Object "`nMultipe installations detected. Silent Installation failed."
}
if ($ActInstall.name.count -eq 1)
{
$InstallPath = $ActInstall.ModuleBase
Write-host 'Found AGMPowerCLI version: ' $ActInstall.Version 'in ' $InstallPath
Remove-Item -Path $InstallPath -Recurse -Force -ErrorAction Stop -Confirm:$false
}
else
{
$InstallPathList = GetPSModulePath
$InstallPath = $InstallPathList[1]
if ( $platform -notmatch "Unix" )
{
$InstallPath = $InstallPath + '\AGMPowerCLI\'
}
else {
$InstallPath = $InstallPath + '/AGMPowerCLI/'
}
}
if ( $platform -notmatch "Unix" )
{
$null = Get-ChildItem -Path $PSScriptRoot\AGMPowerCLI* -Recurse | Unblock-File
}
$null = New-Item -ItemType Directory -Path $InstallPath -Force -ErrorAction Stop
$null = Copy-Item $PSScriptRoot\AGMPowerCLI* $InstallPath -Force -Recurse -ErrorAction Stop
$null = Test-Path -Path $InstallPath -ErrorAction Stop
$commandcheck = get-command -module AGMPowerCLI
if (!($commandcheck))
{
Write-Host 'Silent Installation failed.'
}
else {
Write-Host 'Installed AGMPowerCLI version: ' $ActModuleData.ModuleVersion 'in ' $InstallPath
}
exit
}
if (($args[0] -eq "-silentinstall0") -or ($args[0] -eq "-s0"))
{
silentinstall0
}
if (($args[0] -eq "-silentinstall") -or ($args[0] -eq "-s"))
{
silentinstall
}
if (($args[0] -eq "-silentuninstall") -or ($args[0] -eq "-u"))
{
[Array]$ActInstall = GetAGMPowerCLIInstall
foreach ($Location in ([Array]$ActInstall = GetAGMPowerCLIInstall).ModuleBase)
{
$InstallPath = $Location
Remove-Item -Path $InstallPath -Recurse -Force -ErrorAction Stop -Confirm:$false
}
exit
}
Clear-Host
Write-host 'Detected PowerShell version: ' $hostVersionInfo
Write-host 'Downloaded AGMPowerCLI version:' $ActModuleData.ModuleVersion
Write-host ""
[Array]$ActInstall = GetAGMPowerCLIInstall
if ($ActInstall.Length -gt 0)
{
Write-Host 'Found an existing AGMPowerCLI Module installation in the following locations:'
ReportAGMPowerCLI | Format-Table
write-host ""
Write-host "Upgrade or delete menu (choose a folder to upgrade to"$ActModuleData.ModuleVersion"or choose the delete option):"
$ActInstall += @{
Name = 'Delete All'
Version = 0.0.0.0
ModuleBase = 'DELETE all listed installations of the AGMPowerCLI Module'
}
$InstallPath = InstallMenu -InstallPathList $ActInstall.ModuleBase -InstallAction 'upgrade or delete'
if ($InstallPath.Split(' ')[0] -eq 'DELETE')
{
foreach ($Location in ([Array]$ActInstall = GetAGMPowerCLIInstall).ModuleBase)
{
$InstallPath = $Location
RemoveModuleContent
}
break
}
else
{
RemoveModuleContent
CreateModuleContent
}
}
else
{
Write-Host "Could not find an existing AGMPowerCLI Module installation."
Write-Host "Where would you like to install AGMPowerCLI version"$ActModuleData.ModuleVersion
Write-Host ""
$InstallPath = InstallMenu -InstallPathList (GetPSModulePath) -InstallAction installation
$InstallPath = $InstallPath + '\AGMPowerCLI\'
CreateModuleContent
}
Write-Host -Object "`nAGMPowerCLI Module installation location(s):"
ReportAGMPowerCLI | Format-Table