This repository was archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInstall-CentrifyPlatformSDK.ps1
More file actions
171 lines (155 loc) · 6.2 KB
/
Install-CentrifyPlatformSDK.ps1
File metadata and controls
171 lines (155 loc) · 6.2 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
###########################################################################################
# Centrify Platform PowerShell module - Centrify.Platform.PowerShell Module Installer
#
#
# Author : Fabrice Viguier
# Contact : support AT centrify.com
# Release : 21/01/2016
# Copyright: (c) 2016 Centrify Corporation. 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 Install-PSModule
{
param ([System.String]$Path)
# Install Module by copying Module folder to destination folder
try
{
# Deduct source location from script invocation path
$Source = ("{0}\Centrify.Platform.PowerShell" -f (Split-Path -Parent $PSCommandPath))
# Copy source to module location
$FileCopied = Copy-Item -Path $Source -Destination $Path -Recurse -Force -PassThru -ErrorAction "SilentlyContinue"
if ($FileCopied.Count -gt 0)
{
Write-Host
Write-Host ("{0} files copied." -f $FileCopied.Count)
Write-Host
}
else
{
Write-Error ("No files copied.")
}
# Unblock files to avoid preventing importing the module
Get-ChildItem -Path $Path -Recurse | Unblock-File
}
catch
{
# Unhandled Exception
Throw $_.Exception
}
}
function Remove-PSModule
{
param ([System.String]$Path)
# Delete Module
Remove-Item -Path $Path -Recurse -Force
}
function Test-AdminRight
{
# Get current user identity and principal
$Identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$WindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($Identity)
# Validate that current user is a Local Administrator
if (-not $WindowsPrincipal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
{
Write-Warning ("Installation must be run with Local Administrator privileges. User {0} does not have enough privileges." -f $Identity)
Exit
}
}
function Get-PSModulePath
{
# Get PSModulePath from Environment
$PSModulePath = ([System.Environment]::GetEnvironmentVariable("PSModulePath")) -Split ';' | Where-Object { $_ -match "\\Centrify\\PowerShell\\" }
if ([System.String]::IsNullOrEmpty($PSModulePath))
{
Write-Host "No Centrify Platform PowerShell Module detected on this system."
if (-not (Test-Path -Path "C:\Program Files\Centrify\"))
{
# Create full path
mkdir "C:\Program Files\Centrify"
mkdir "C:\Program Files\Centrify\PowerShell"
}
else
{
# Create PowerShell folder
if (-not (Test-Path -Path "C:\Program Files\Centrify\PowerShell"))
{
mkdir "C:\Program Files\Centrify\PowerShell"
}
}
$PSModulePath = "C:\Program Files\Centrify\PowerShell\"
# Set PSModulePath into Machine Environment
[System.Environment]::SetEnvironmentVariable("PSModulePath", ("{0};{1}" -f [System.Environment]::GetEnvironmentVariable("PSModulePath"), $PSModulePath), "Machine")
Write-Warning "PSModulePath environmnet variable has been updated. Operating System may need to be rebooted for change to be taken into account."
}
else
{
Write-Host ("Centrify Platform PowerShell module detected on this system under '{0}'" -f $PSModulePath)
}
# Return Path
return $PSModulePath
}
##############
# Main Logic #
##############
# Validate Local Admin privileges
Test-AdminRight
# Starting installation
Write-Host
Write-Host "################################################################"
Write-Host "# Centrify.Platform.PowerShell Module Installer #"
Write-Host "################################################################"
Write-Host
$PSModulePath = Get-PSModulePath
$InstallationPath = ("{0}Centrify.Platform.PowerShell" -f $PSModulePath)
Write-Host ("Centrify Platform PowerShell module will be using Installation path:`n`t'{0}'" -f $InstallationPath)
if (Test-Path -Path $InstallationPath)
{
# Build Menu
$Title = "The Centrify Platform PowerShell module is already installed."
$Message = ("Choose action to perform:`n")
$Message += ("[R] - Repair/Upgrade Module by deleting and re-installing all files.`n")
$Message += ("[U] - Uninstall and exit.`n")
$Message += ("[C] - Cancel and exit.`n")
$Choice0 = New-Object System.Management.Automation.Host.ChoiceDescription "&Repair", "Repair Module"
$Choice1 = New-Object System.Management.Automation.Host.ChoiceDescription "&Uninstall", "Uninstall and exit"
$Choice2 = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel", "Cancel and exit"
$Options = [System.Management.Automation.Host.ChoiceDescription[]]($Choice0, $Choice1, $Choice2)
# Prompt for choice
$Prompt = $Host.UI.PromptForChoice($Title, $Message, $Options, 2)
switch ($Prompt)
{
0 # Repare
{
Write-Host "Repairing/Upgrading Module."
# Remove Module
Remove-PSModule -Path $InstallationPath
Write-Host ("Centrify Platform PowerShell module '{0}' deleted" -f $InstallationPath)
# Installing Module
Install-PSModule -Path $InstallationPath
Write-Host ("Centrify Platform PowerShell module installed under '{0}'" -f $PSModulePath)
}
1 # Uninstall
{
Write-Host "Uninstalling Module."
# Remove Module
Remove-PSModule -Path $InstallationPath
Write-Host ("Centrify Platform PowerShell module '{0}' deleted" -f $InstallationPath)
Exit
}
2 # Exit
{
Write-Host "Operation canceled.`n"
Exit
}
}
}
else
{
Write-Host "Installing module."
# Installing Module
Install-PSModule -Path $InstallationPath
Write-Host ("Centrify Platform PowerShell module installed under '{0}'" -f $PSModulePath)
}
# Done.