-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.ps1
More file actions
155 lines (126 loc) · 3.93 KB
/
Utils.ps1
File metadata and controls
155 lines (126 loc) · 3.93 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
<#
.SYNOPSIS
Utility script for common helper functions
.DESCRIPTION
PowerShell script to house common utility functions.
This is meant to be sourced in other scripts and does not
perform any task in itself
.EXAMPLE
Source this file in other scripts using the "." notation - ". .\Utils.ps1"
.NOTES
Author: HardcodeCoder
Created: 19 May 2025
Last Modified: 01 June 2025
Version: 1.0.1
Required Modules: PowerShell Remoting
.LINK
https://docs.microsoft.com/en-us/powershell/
#>
# Global temporary working directory
$WorkingDir = Join-Path -Path $env:TEMP -ChildPath "BetterWindows"
# Invoke script as administrator
function Invoke-AsAdministrator {
param (
[string]$ScriptPath
)
if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
return
}
try {
Start-Process powershell.exe -ArgumentList ("-NoProfile -NoExit -ExecutionPolicy Bypass -File `"{0}`"" -f $ScriptPath) -Verb RunAs -WindowStyle Maximized
Exit
}
catch {
Write-Warning "Failed to run as Administrator. Please rerun with elevated privileges."
Read-Host "Press any key to exit."
Exit
}
}
# Initialize powershell window for administrator look
function Initialize-PowershellWindow {
$Host.UI.RawUI.WindowTitle = "Better Windows - Administrator"
$Host.UI.RawUI.BackgroundColor = "Black"
$Host.PrivateData.ProgressBackgroundColor = "Black"
$Host.PrivateData.ProgressForegroundColor = "White"
Clear-Host
}
# Write text to standard output but centered
function Write-CenteredText {
param (
[string]$Text,
[string]$ForegroundColor = 'White'
)
$consoleWidth = [Console]::WindowWidth
$padding = [math]::Floor(($consoleWidth - $Text.Length) / 2)
$paddedText = ' ' * $padding + $Text
Write-Host $paddedText -ForegroundColor $ForegroundColor
}
# Write current task header
function Write-TaskHeader {
param (
[string]$TaskName
)
$decorator = '-' * 62
$text = ' ' * ((62 - $TaskName.Length) / 2) + $TaskName
Write-Host $decorator
Write-Host $text
Write-Host $decorator
Write-Host ""
}
# Write task footer divider
function Write-TaskFooter {
$decorator = '-' * 62
Write-Host $decorator
Write-Host ""
}
# Write exception message as warning
function Write-UnhandledException {
param (
[string]$Description,
[System.Exception]$Exception
)
Write-Warning "$Description - $($Exception.Message)"
Write-Warning $Exception.StackTrace
}
# Deserialize json from file to powershell object
function ConvertFrom-JsonFie {
param (
[string]$Path
)
if ($Path -eq '' -Or !(Test-Path -Path $Path)) {
Write-Host "File not found: $Path" -ForegroundColor Red
return $null
}
$json = Get-Content -Path $Path -Raw
return ConvertFrom-Json -InputObject $json
}
# Utility to download file silently to improve downlad speed
function Invoke-FileDownload {
param (
[string]$Uri,
[string]$OutFile
)
try {
$directory = Split-Path -Path $OutFile -Parent
if (!(Test-Path -Path $directory)) {
New-Item -ItemType Directory -Path $directory -Force | Out-Null
}
Write-Host "Downloading: $Uri"
Invoke-RestMethod -Uri $Uri -OutFile $OutFile
Write-Host "Download location: $OutFile"
}
catch {
Write-UnhandledException -Description "Unable to downlad file" -E $_.Exception
}
}
# Cleanup working directory
function Remove-WorkingDir {
if (Test-Path -Path $WorkingDir) {
Get-ChildItem -Path $WorkingDir -Recurse | Remove-Item -Recurse -Force
Remove-Item -Path $WorkingDir -Force
}
}
# Check if the host machine is a mobile device
function Test-IsMobileDevice {
return (Get-WmiObject -Class Win32_ComputerSystem).PCSystemType -eq 2
}