-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgofetch.ps1
175 lines (160 loc) · 6.58 KB
/
gofetch.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
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
<#
.SYNOPSIS
Downloads the latest version of the Loaner script from the GitHub repository and runs it.
.DESCRIPTION
This script downloads the latest version of the Loaner script from the GitHub repository and runs it.
The download location can be specified using the --download option, which accepts a location key or a custom path.
If no download location is specified, the script will default to the user's temp folder.
The --location option can be used to display the path for a given location key.
The --help option displays usage information.
.PARAMETER download
Specifies the download location key or a custom path.
If a location key is specified, the script will use the predefined download location associated with the key.
If a custom path is specified, the script will use the specified path as the download location.
.PARAMETER location
Displays the path for a given location key.
.PARAMETER help
Displays usage information.
.EXAMPLE
.\pulldown.ps1 --download desktop
Downloads the latest version of the Loaner script to the user's desktop.
.EXAMPLE
.\pulldown.ps1 --download C:\Downloads
Downloads the latest version of the Loaner script to the specified custom path.
#>
# Initial parameter setup with defaults
# Run without asking = 1 ; Run after asking = 0
$Run = "1"
# Default to user's temp folder
$DownloadLocation = [System.IO.Path]::GetTempPath()
# Predefined download locations
$LocationMap = @{
#admin folder
'desktop' = [System.Environment]::GetFolderPath('Desktop')
'documents' = [System.Environment]::GetFolderPath('MyDocuments')
'loaner' = Join-Path -Path $env:USERPROFILE -ChildPath 'Downloads\Loaner_Updater\'
}
# Ensure the Temp directory exists
$TempPath = [System.IO.Path]::GetTempPath()
if (-Not (Test-Path -Path $TempPath)) {
New-Item -ItemType Directory -Path $TempPath | Out-Null
Write-Host "Temporary directory created: $TempPath" -ForegroundColor Green
}
function Show-Error {
Write-Host "---------------------------------------------------------" -ForegroundColor Red
Write-Host "---------------------------------------------------------" -ForegroundColor Red
Write-Host "-----------------HEY, THERE'S AN ERROR!!-----------------" -ForegroundColor Red
Write-Host "---------------------------------------------------------" -ForegroundColor Red
Write-Host "---------------------------------------------------------" -ForegroundColor Red
}
function CheckFolder {
param (
[string]$folderPath
)
if (-Not (Test-Path -Path $folderPath)) {
Write-Host "ERROR: The folder '$folderPath' does not exist." -ForegroundColor Red
$choice = Read-Host "Do you want to create the folder? (Y/N)"
if ($choice -eq 'Y' -or $choice -eq 'y') {
New-Item -ItemType Directory -Path $folderPath | Out-Null
Write-Host "Folder created: $folderPath" -ForegroundColor Green
}
else {
Write-Host "Operation cancelled. Exiting script..." -ForegroundColor Yellow
exit
}
}
}
# Parsing custom command-line arguments
for ($i = 0; $i -lt $args.Count; $i++) {
switch -Regex ($args[$i]) {
'^--?help$' {
# Display usage information and exit
Write-Host "Usage: down4.ps1 [options]"
Write-Host "Options:"
Write-Host " --download, -download, --down, -down Specify the download location key or a custom path"
Write-Host " --help, -help Display this help message and exit"
Write-Host " --location, -location Display the path for a given location key"
Write-Host ""
Write-Host "Available location keys:"
$LocationMap.Keys | ForEach-Object { Write-Host " $_" }
exit
}
'^--?(down(load)?|dl)$' {
$i++ # Move to the next argument which should be the download location key or path
$locationKey = $args[$i]
if ($LocationMap.ContainsKey($locationKey)) {
$DownloadLocation = $LocationMap[$locationKey]
}
else {
$DownloadLocation = $TempPath
}
}
'^--?location$' {
$i++ # Move to the next argument which should be the location key
$locationKey = $args[$i]
if ($LocationMap.ContainsKey($locationKey)) {
Write-Host "Location for '$locationKey': $($LocationMap[$locationKey])"
}
else {
Write-Host "Invalid location key: '$locationKey'. Available keys are:" -ForegroundColor Red
$LocationMap.Keys | ForEach-Object { Write-Host " $_" }
}
exit
}
}
}
function Invoke-FileDownload {
$filename = "loaner.ps1"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$url = "https://raw.githubusercontent.com/Nicholas-Stull/Loaner-Updater/main/loaner.ps1"
$destinationPath = Join-Path -Path $DownloadLocation -ChildPath $filename
Write-Host "Downloading to: $destinationPath"
# Perform the download
Invoke-WebRequest -Uri $url -Headers $headers -OutFile $destinationPath
Write-Output "Scanning for file..."
# Check if the file exists
if (Test-Path $destinationPath) {
Write-Output "Download complete."
RunLoanerQuest -destinationPath $destinationPath # Call RunLoanerQuest with the downloaded file path
}
else {
Write-Output "Download failed or the file does not exist."
}
}
function RunLoanerQuest {
param (
[string]$destinationPath # Add a parameter to accept the file path
)
$runloanerstring = Out-String -InputObject $destinationPath
Write-Host $runloanerstring
if ($Run -eq "1") {
Invoke-Expression -Command "$runloanerstring"
}
if ($Run -eq "0"){
$ChoiceText = "Run?"
Write-Host $ChoiceText -ForegroundColor Green
$choice = Read-Host "(Y/N)"
if ($choice -eq 'Y' -or $choice -eq 'y') {
Invoke-Expression -Command "$runloanerstring"
Write-Warning "'Should' Restart after run"
shutdown /r
}
else {
Write-Host "Operation cancelled. Exiting script..." -ForegroundColor Yellow
exit
} }
}
function Main {
Write-Host "Downloading and Running..."
CheckFolder $DownloadLocation
Invoke-FileDownload -url $url -Headers $headers -destination $DownloadLocation
RunLoanerQuest
}
try {
Main
}
catch {
Show-Error
Write-Error "An error occurred: $_"
}