-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstall-Module.ps1
More file actions
215 lines (175 loc) · 8.5 KB
/
Install-Module.ps1
File metadata and controls
215 lines (175 loc) · 8.5 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
<#
.SYNOPSIS
Installs the ClientTool module from GitHub to your PowerShell modules directory
.DESCRIPTION
Downloads and installs the ClientTool module from GitHub to your PowerShell modules folder,
making it available for import from any PowerShell session.
.PARAMETER Scope
Install for CurrentUser or AllUsers (requires admin for AllUsers)
.PARAMETER FromGitHub
Download from GitHub instead of using local files
.PARAMETER Version
Specific version to download from GitHub (e.g., 'v1.1.0'). Defaults to latest (main branch)
.EXAMPLE
.\Install-Module.ps1
Installs from local files for current user
.EXAMPLE
.\Install-Module.ps1 -FromGitHub
Downloads and installs latest version from GitHub for current user
.EXAMPLE
.\Install-Module.ps1 -FromGitHub -Version v1.1.0
Downloads and installs specific version from GitHub
.EXAMPLE
.\Install-Module.ps1 -Scope AllUsers
Installs for all users (requires admin)
.EXAMPLE
irm https://raw.githubusercontent.com/BackupNerd/ClientTool/main/Install-Module.ps1 | iex
One-liner to download and run this script directly from GitHub
#>
[CmdletBinding()]
param(
[Parameter()]
[ValidateSet('CurrentUser', 'AllUsers')]
[string]$Scope = 'CurrentUser',
[Parameter()]
[switch]$FromGitHub,
[Parameter()]
[string]$Version = 'main'
)
$moduleName = 'ClientTool'
$githubRepo = 'BackupNerd/ClientTool'
# If running from web (no $PSScriptRoot), force GitHub download
if ([string]::IsNullOrWhiteSpace($PSScriptRoot)) {
Write-Host "Running from web - downloading from GitHub..." -ForegroundColor Cyan
$FromGitHub = $true
}
$sourceDir = $PSScriptRoot
# Determine target directory based on scope
if ($Scope -eq 'AllUsers') {
$targetBase = "$env:ProgramFiles\PowerShell\Modules"
# Check for admin rights
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Error "Installing for AllUsers requires administrator privileges. Please run as administrator or use -Scope CurrentUser"
exit 1
}
}
else {
# Get the first user-scoped module path from PSModulePath
$userModulePaths = @($env:PSModulePath -split ';' | Where-Object {
$_ -like "*$env:USERNAME*" -or $_ -like "*Documents*"
} | Where-Object { Test-Path $_ -ErrorAction SilentlyContinue })
if ($userModulePaths.Count -gt 0) {
$targetBase = $userModulePaths[0].ToString().Trim()
} else {
# Fallback to hardcoded path if nothing found in PSModulePath
if ($PSVersionTable.PSVersion.Major -ge 7) {
$targetBase = "$HOME\Documents\PowerShell\Modules"
} else {
$targetBase = "$HOME\Documents\WindowsPowerShell\Modules"
}
}
}
$targetDir = Join-Path -Path $targetBase -ChildPath $moduleName
Write-Host "`n╔══════════════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ ClientTool Module Installation ║" -ForegroundColor Cyan
Write-Host "╚══════════════════════════════════════════════════════════════════════╝`n" -ForegroundColor Cyan
# Download from GitHub if requested or running from web
if ($FromGitHub) {
Write-Host "📥 Downloading from GitHub..." -ForegroundColor Yellow
# Determine download URL
if ($Version -eq 'main') {
$downloadUrl = "https://github.com/$githubRepo/archive/refs/heads/main.zip"
$extractFolder = "$moduleName-main"
} else {
$downloadUrl = "https://github.com/$githubRepo/archive/refs/tags/$Version.zip"
$extractFolder = "$moduleName-$($Version.TrimStart('v'))"
}
$tempZip = Join-Path $env:TEMP "ClientTool-Download.zip"
$tempExtract = Join-Path $env:TEMP "ClientTool-Extract"
try {
# Download
Write-Host " Downloading from: $downloadUrl" -ForegroundColor Gray
Invoke-WebRequest -Uri $downloadUrl -OutFile $tempZip -UseBasicParsing
Write-Host " ✓ Downloaded" -ForegroundColor Green
# Extract
Write-Host " Extracting..." -ForegroundColor Gray
if (Test-Path $tempExtract) {
Remove-Item $tempExtract -Recurse -Force
}
Expand-Archive -Path $tempZip -DestinationPath $tempExtract -Force
Write-Host " ✓ Extracted" -ForegroundColor Green
# Set source directory to extracted folder
$sourceDir = Join-Path $tempExtract $extractFolder
if (-not (Test-Path $sourceDir)) {
Write-Error "Extracted folder not found: $sourceDir"
exit 1
}
}
catch {
Write-Error "Failed to download from GitHub: $_"
exit 1
}
}
Write-Host "📦 Installing ClientTool module..." -ForegroundColor Cyan
Write-Host " Source: $sourceDir" -ForegroundColor Gray
Write-Host " Target: $targetDir" -ForegroundColor Gray
Write-Host ""
# Create target directory if it doesn't exist
if (-not (Test-Path $targetDir)) {
Write-Host " Creating directory..." -ForegroundColor Yellow
New-Item -Path $targetDir -ItemType Directory -Force | Out-Null
}
else {
Write-Host " Removing existing installation..." -ForegroundColor Yellow
Remove-Item -Path "$targetDir\*" -Recurse -Force
}
# Copy all module files
Write-Host " Copying files..." -ForegroundColor Yellow
Copy-Item -Path "$sourceDir\*" -Destination $targetDir -Recurse -Force -Exclude @('*.backup', '.git', '.gitignore', 'Publish-ModuleToGitHub.ps1')
# Clean up downloaded files
if ($FromGitHub) {
Remove-Item $tempZip -Force -ErrorAction SilentlyContinue
Remove-Item $tempExtract -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Host " ✓ Installation complete!" -ForegroundColor Green
Write-Host "`n✅ Module installed successfully!`n" -ForegroundColor Green
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Gray
# Test import
Write-Host "`n🔄 Testing module import..." -ForegroundColor Yellow
# Remove old module from session if loaded
Remove-Module ClientTool -Force -ErrorAction SilentlyContinue
try {
# Import from the installed location
Import-Module $targetDir -Force -ErrorAction Stop
$module = Get-Module ClientTool
$commands = Get-Command -Module ClientTool
Write-Host " ✓ Module imported successfully!" -ForegroundColor Green
Write-Host " ✓ Version: $($module.Version)" -ForegroundColor Green
Write-Host " ✓ Available commands: $($commands.Count)" -ForegroundColor Green
}
catch {
Write-Host " ✗ Failed to import module!" -ForegroundColor Red
Write-Host " Error: $_" -ForegroundColor Red
Write-Host "`n The module files have been installed to: $targetDir" -ForegroundColor Yellow
Write-Host " Try running: Import-Module '$targetDir' -Force" -ForegroundColor Yellow
exit 1
}
Write-Host "`n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Gray
Write-Host "`n📚 Getting Started:" -ForegroundColor Cyan
Write-Host ""
Write-Host " Import the module:" -ForegroundColor White
Write-Host " Import-Module ClientTool" -ForegroundColor Green
Write-Host ""
Write-Host " See all available commands:" -ForegroundColor White
Write-Host " Get-Command -Module ClientTool" -ForegroundColor Green
Write-Host ""
Write-Host " Get help on any command:" -ForegroundColor White
Write-Host " Get-Help Get-ClientToolStatus -Full" -ForegroundColor Green
Write-Host ""
Write-Host " Quick status check:" -ForegroundColor White
Write-Host " Get-ClientToolStatus" -ForegroundColor Green
Write-Host ""
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Gray
Write-Host "`n📖 Documentation: https://github.com/$githubRepo" -ForegroundColor Cyan
Write-Host ""