-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-usersapp.ps1
More file actions
160 lines (124 loc) · 6.26 KB
/
install-usersapp.ps1
File metadata and controls
160 lines (124 loc) · 6.26 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
<#
.SYNOPSIS
UsersApp installation script
.DESCRIPTION
UsersApp is a PowerShell 7.1+ app composed by LocalUsers module and a separate GUI script.
This script:
- install/update PS 7.1+
- install/update LocalUsers Module
- install UsersApp
This script is meant to be luanched with command: irm https://github.com/HumanAgainstMachine/LocalUsers/releases/latest/download/install-GUL.ps1 | iex
#>
# The baseurl where to download everething needed to install UsersApp
$baseUrl = "https://github.com/HumanAgainstMachine/UsersApp/releases/latest/download/"
# Create UsersApp Roaming Folder
$usersAppPath = Join-Path $env:APPDATA "UsersApp"
New-Item -ItemType Directory -Force -Path $usersAppPath | Out-Null
# Download this script
$thisScriptUrl = $baseUrl + "install-usersapp.ps1"
$thisScriptPath = Join-Path $usersAppPath "install-usersapp.ps1"
Invoke-WebRequest -Uri $thisScriptUrl -OutFile $thisScriptPath
$ps7Path = "C:\Program Files\PowerShell\7\pwsh.exe"
# Case PS5 is running
if ($PSVersionTable.PSVersion.ToString().substring(0,1) -eq '5') {
# PS7 installed test
if (-not (Test-Path -Path $ps7Path)) {
# install PS7.1+
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$thisScriptPath`"" -Verb RunAs
exit
}
Write-Host "Installing PowerShell 7..."
$scriptPath = "$env:TEMP\install-powershell.ps1"
Invoke-WebRequest -Uri "https://aka.ms/install-powershell.ps1" -OutFile $scriptPath
# Run system-wide silent install using MSI
& $scriptPath -UseMSI -Quiet
Write-Host "PowerShell 7 installed successfully."
}
write-host "Launch PS7"
Start-Process pwsh.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$thisScriptPath`"" -Verb RunAs
exit
}
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process pwsh.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$thisScriptPath`"" -Verb RunAs
exit
}
# update PS7.1+
$installedVersion = $PSVersionTable.PSVersion.ToString()
$releasesUrl = "https://api.github.com/repos/PowerShell/PowerShell/releases/latest"
try {
$response = Invoke-RestMethod -Uri $releasesUrl -UseBasicParsing
$latestVersion = $response.tag_name.TrimStart("v")
} catch {
Write-Error "Failed to check latest version from GitHub."
}
Write-Host "Installed version: $installedVersion"
Write-Host "Latest version: $latestVersion"
# Compare versions and install if needed
if ($installedVersion -ne $latestVersion) {
Write-Host "Updating PowerShell to version $latestVersion..."
$scriptPath = "$env:TEMP\install-powershell.ps1"
Invoke-WebRequest -Uri "https://aka.ms/install-powershell.ps1" -OutFile $scriptPath
# Run system-wide silent install using MSI
& $scriptPath -UseMSI -Quiet
Write-Host "PowerShell $latestVersion installed successfully."
} else {
Write-Host "PowerShell is already up to date."
}
# Install/Update LocalUsers module
if ($lUsers = Get-Module -ListAvailable -Name LocalUsers) {
Write-Host "LocalUsers module is already installed. Checking for updates..." -ForegroundColor Green
$currentModule = $lUsers | Sort-Object Version -Descending | Select-Object -First 1
$onlineModule = Find-Module -Name LocalUsers -Repository PSGallery
if ($onlineModule.Version -gt $currentModule.Version) {
Write-Host "Updating LocalUsers module from version $($currentModule.Version) to $($onlineModule.Version)..." -ForegroundColor Yellow
Update-Module -Name LocalUsers -Force
Write-Host "LocalUsers module has been updated to version $($onlineModule.Version)." -ForegroundColor Green
} else {
Write-Host "LocalUsers module is already at the latest version ($($currentModule.Version))." -ForegroundColor Green
}
} else {
Write-Host "Installing LocalUsers module from PSGallery..." -ForegroundColor Yellow
Install-Module -Name LocalUsers -Repository PSGallery -Force -Scope AllUsers
if ($lUsers = Get-Module -ListAvailable -Name LocalUsers) {
$installedModule = $lUsers | Sort-Object Version -Descending | Select-Object -First 1
Write-Host "LocalUsers module has been successfully installed (Version: $($installedModule.Version))." -ForegroundColor Green
} else {
Write-Host "Failed to install LocalUsers module. Please install manually." -ForegroundColor Red
}
}
$usersAppUrl = $baseUrl + "usersapp.ps1"
# Create a script that the shortcut links to in order to prevent the shortcut from being flagged as a virus.
$usersAppLaunchScript = @"
Start-Process pwsh.exe -Verb RunAs -ArgumentList '-Command "irm $usersAppUrl | iex"'
"@
$usersAppLaunchScriptPath = Join-Path $usersAppPath "LaunchUsersApp.ps1"
Set-Content -Path $usersAppLaunchScriptPath -Value $usersAppLaunchScript
# Download icon
$iconUrl = $baseUrl + "usersapp.ico"
$iconPath = Join-Path $usersAppPath "usersapp.ico"
try {
Invoke-WebRequest -Uri $iconUrl -OutFile $iconPath
Write-Host "Icon downloaded successfully." -ForegroundColor Green
} catch {
Write-Host "Failed to download icon." -ForegroundColor Red
$iconPath = "$ps7Path,0" # Fallback to PowerShell icon
}
# Create desktop shortcut for UsersApp
$desktopPath = [Environment]::GetFolderPath("Desktop")
$shortcutPath = Join-Path $desktopPath "UsersApp.lnk"
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($shortcutPath)
$Shortcut.TargetPath = $ps7Path
$Shortcut.Arguments = "-ExecutionPolicy Bypass -File `"$usersAppLaunchScriptPath`""
$Shortcut.IconLocation = $iconPath
$Shortcut.Description = "Launch UsersApp"
$Shortcut.Save()
# Add "Run as administrator" to the shortcut
$bytes = [System.IO.File]::ReadAllBytes($shortcutPath)
$bytes[0x15] = $bytes[0x15] -bor 0x20
[System.IO.File]::WriteAllBytes($shortcutPath, $bytes)
Write-Host "Installation completed successfully!" -ForegroundColor Green
# Pause before closing
Write-Host "`nPress any key to close..."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null