forked from rubrikinc/rubrik-sdk-for-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Install-Rubrik.ps1
119 lines (107 loc) · 3.04 KB
/
Install-Rubrik.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
function GetRubrikInstall
{
# Returns the known installation locations for the Rubrik Module
return Get-Module -ListAvailable -Name Rubrik -ErrorAction SilentlyContinue | Select-Object -Property Name, Version, ModuleBase
}
function GetPSModulePath
{
# Returns all available PowerShell Module paths
return $env:PSModulePath.Split(';')
}
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
try
{
Remove-Item -Path $InstallPath -Recurse -Force -ErrorAction Stop -Confirm:$true
}
catch
{
throw "$($_.ErrorDetails)"
}
}
function CreateModuleContent
{
# Attempts to create a new folder and copy over the Rubrik Module contents
try
{
$null = New-Item -ItemType Directory -Path $InstallPath -Force -ErrorAction Stop
$null = Copy-Item $PSScriptRoot\Rubrik\* $InstallPath -Force -Recurse -ErrorAction Stop
$null = Test-Path -Path $InstallPath -ErrorAction Stop
Write-Host -Object "`nInstallation successful."
}
catch
{
throw $_
}
}
function ReportRubrikModule
{
# Removes the Rubrik Module from the active session and displays a list of all current install locations
Remove-Module -Name Rubrik -ErrorAction SilentlyContinue
GetRubrikInstall
}
### Code
Clear-Host
[Array]$RubrikInstall = GetRubrikInstall
if ($RubrikInstall.Length -gt 0)
{
Write-Warning -Message 'Found an existing Rubrik Module installed.' -WarningAction Continue
$RubrikInstall += @{
Name = 'Delete All'
Version = 0.0.0.0
ModuleBase = 'DELETE all listed installations of the Rubrik Module'
}
$InstallPath = InstallMenu -InstallPathList $RubrikInstall.ModuleBase -InstallAction 'upgrade or delete'
if ($InstallPath.Split(' ')[0] -eq 'DELETE')
{
foreach ($Location in ([Array]$RubrikInstall = GetRubrikInstall).ModuleBase)
{
$InstallPath = $Location
RemoveModuleContent
}
break
}
else
{
RemoveModuleContent
CreateModuleContent
}
}
else
{
Write-Warning -Message 'Could not find an existing Rubrik Module installation.' -WarningAction Continue
$InstallPath = InstallMenu -InstallPathList (GetPSModulePath) -InstallAction installation
$InstallPath = $InstallPath + '\Rubrik\'
CreateModuleContent
}
Write-Host -Object "`nRubrik Module installation location(s):"
ReportRubrikModule | Format-Table