-
Notifications
You must be signed in to change notification settings - Fork 0
/
loaner3.ps1
345 lines (307 loc) · 11.4 KB
/
loaner3.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
$VerbosePreference = "Continue"
$LoanerUseByDate = Get-Date -Year 2024 -Month 12 -Day 20 -Hour 23 -Minute 59 -Second 00
$script:flashDriveLetter = ""
# log messages
$LogBreakMessage = "---------------------------------------"
function UpdateFlashDriveLocation {
# Save the current verbose preference
# Disable verbose output
$VerbosePreference = 'SilentlyContinue'
# Get the list of removable drives
$removableDrives = Get-CimInstance -Query "SELECT * FROM Win32_DiskDrive WHERE MediaType='Removable Media'"
if ($removableDrives) {
foreach ($drive in $removableDrives) {
$partitions = Get-CimAssociatedInstance -InputObject $drive -ResultClassName Win32_DiskPartition
foreach ($partition in $partitions) {
$logicalDisks = Get-CimAssociatedInstance -InputObject $partition -ResultClassName Win32_LogicalDisk
foreach ($logicalDisk in $logicalDisks) {
$driveLetter = $logicalDisk.DeviceID
if ($driveLetter) {
$script:flashDriveLetter = "$driveLetter\"
#break 3 # Exit all loops once a drive is found
}
}
}
}
}
else {
Write-Verbose "No removable drive found."
}
# Restore the original verbose preference
$VerbosePreference = $oldVerbosePreference
return $script:flashDriveLetter
}
function makefolder {
param (
[Parameter(Mandatory = $true)]
[string]$foldername
)
if (!(Test-Path $foldername)) {
New-Item -ItemType Directory -Force -Path $foldername
}
}
$StandardDate = "MM-dd-yyyy_HH-mm-ss"
$LongDate = "dddd, MMMM dd'th', yyyy HH:mm:ss"
$ExpireDate = "MM-dd-yyyy HH:mm:ss"
$TimeStampDate = "MM-dd-yyyy HH:mm:ss.fff"
$ExpireDate = (Get-Date).ToString($ExpireDate)
$FancyDate = (Get-Date).ToString($LongDate)
$ComputerLogName = $env:COMPUTERNAME.ToLower()
function GetLogFolder {
$driveLetter = UpdateFlashDriveLocation
if (!$driveLetter) {
Write-Host "No flash drive found. Exiting function."
return
}
$LoanerDir = Join-Path "Loaner" "Log"
$logDir = Join-Path $script:flashDriveLetter $LoanerDir
makefolder $logDir
$CompNameUnderScore = "$env:COMPUTERNAME" + "_"
$LogFileNamePrefix = "$CompNameUnderScore"
$LogFileExtension = ".log"
$LogFileName = $LogFileNamePrefix + (Get-Date).ToString($StandardDate) + $LogFileExtension
$logFile = Join-Path $logDir $LogFileName
return $logFile
return $ComputerLogName
return $logDir
}
$logFile = GetLogFolder
$LogDir = GetLogFolder
if (-not $logFile) {
Write-Host "Log file could not be initialized because no valid drive was found."
return
}
# Loaner User Variables
$LoanerUser = "smcloaner"
$LoanerPassword = "DoitLoan1@"
$LoanerPass = ConvertTo-SecureString $LoanerPassword -AsPlainText -Force # Super strong plain text password here (yes this isn't secure at all)
$LoanerName = "SMC Loaner"
$LoanerDesc = "Local Account for the SMC loaner's" # Description of the account
$LoanerExpireDate = $LoanerUseByDate.ToString("MM-dd-yyyy HH:mm:ss")
$LoginMessage = "Loaner Login: Username : .\$LoanerUser | Loaner Password : $LoanerPassword"
function Write-Log {
param(
[Parameter(Mandatory = $true)][string] $message,
[Parameter(Mandatory = $false)]
[ValidateSet("INFO", "WARN", "ERROR")]
[string] $level = "INFO"
)
$timestamp = (Get-Date).ToString($TimeStampDate)
$logMessage = "[$timestamp] [$level] - $message"
# Retry mechanism for writing to the log file
$maxRetries = 5
$retryCount = 0
$success = $false
while (-not $success -and $retryCount -lt $maxRetries) {
try {
if ($logFile) {
Add-Content -Path $logFile -Value $logMessage
$success = $true
}
}
catch {
# Increment retry count and wait for a short time before retrying
$retryCount++
Start-Sleep -Milliseconds 200
}
}
if (-not $success) {
Write-Host "Failed to write to the log after $retryCount attempts."
}
}
function Write-Verbose {
param([string]$message,
[ValidateSet("INFO", "WARN", "ERROR")]
[string] $level = "INFO"
)
Microsoft.PowerShell.Utility\Write-Verbose $message
Write-Log -message $message -level "INFO"
}
$LoanerPrograms = @(
"Statdisk 13 version *.*.*",
"Microsoft Edge*",
"Microsoft Edge Update*",
"Microsoft Edge WebView2 Runtime*",
"PaperCut MF Client*",
"Request Handler Agent*",
"Realtek USB Audio*",
"Patch Management Service Controller*",
"Intel*Software Installer*",
"FileWave Client*",
"Dell Command | Update*",
"Adobe Refresh Manager*",
"Adobe Acrobat Reader DC*",
"Microsoft Visual C++ 2019 X86 Additional Runtime - *.*.*",
"Dell SupportAssist OS Recovery Plugin for Dell Update*",
"Windows Agent*",
"Windows 10 Update Assistant*",
"Microsoft Visual C++ 2015-2019 Redistributable (x86) - *.*.*",
"File Cache Service Agent*",
"Microsoft Visual C++ 2019 X86 Minimum Runtime - *.*.*",
"Realtek Audio Driver*",
"MSP Core Agent*",
"Alertus Desktop*"
)
function SoftwareInstalled {
param (
[string]$softwareName,
[string]$vendorName
)
$softwareInstalled = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -like $softwareName -and $_.Vendor -eq $vendorName }
if ($softwareInstalled) {
Write-Verbose "$softwareName is installed."
}
else {
Write-Verbose "$softwareName is not installed."
}
}
function ProgramCheck {
# Get list of installed programs
$installedPrograms = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName | Where-Object { $null -ne $_.DisplayName } | Select-Object -ExpandProperty DisplayName
# Compare lists and find the difference
$notOnLoanerList = @()
foreach ($program in $installedPrograms) {
$matched = $false
foreach ($LoanerProgram in $LoanerPrograms) {
if ($program -like $LoanerProgram) {
$matched = $true
break
}
}
if (-not $matched) {
$notOnLoanerList += $program
}
}
# Check if there is a difference
if ($notOnLoanerList.Count -eq 0) {
Write-Verbose "All Good! No new programs installed."
}
else {
# Output programs not on the original list
Write-Verbose "Programs installed that are not on the original list:"
Write-Verbose "$notOnLoanerList"
}
}
function Remove_LoanerUser {
process {
try {
#removes smcloaner user from computer cleaning files up
Remove-LocalUser $LoanerUser
Write-Verbose "$LoanerUser deleted"
}
catch {
Write-Verbose "Deleting local account failed" -level "ERROR"
}
}
}
function LoanerUser_Make {
<# If you want to re-enable it use the following commands: net user username /expires:never #>
try {
# Create new local user
New-LocalUser -Name "$LoanerUser" -Password $LoanerPass -FullName "$LoanerName" -Description "$LoanerDesc" -ErrorAction SilentlyContinue -ErrorVariable userError
if ($userError) {
Write-Verbose "Failed to create user: $userError" -level "ERROR"
}
Write-Verbose "$LoanerUser local user created" -level "ERROR"
Write-Verbose "Loaner Login: $LoginMessage"
# Add new user to Administrators group
Add-LocalGroupMember -Group "Administrators" -Member "$LoanerUser" -ErrorAction SilentlyContinue -ErrorVariable groupError
if ($groupError) {
Write-Verbose "Failed to add user to Administrators group: $groupError" -level "ERROR"
}
Write-Verbose "$LoanerUser added to the Administrators group"
# Set account expiration date
$LoanerUser | Set-LocalUser -AccountExpires $LoanerUseByDate -ErrorAction SilentlyContinue -ErrorVariable expireError
if ($expireError) {
Write-Verbose "Failed to set account expiration: $expireError" -level "ERROR"
}
Write-Verbose "$LoanerUser account set to expire on $LoanerExpireDate"
# Set password to never expire
$LoanerUser | Set-LocalUser -PasswordNeverExpires $true -ErrorAction SilentlyContinue -ErrorVariable neverExpireError
if ($neverExpireError) {
Write-Verbose "Failed to set password to never expire: $neverExpireError" -level "ERROR"
}
Write-Verbose "$LoanerUser password set to never expire"
}
catch {
Write-Verbose "Error occurred: $_" -level "ERROR"
}
}
##--Bypass OOBE + Privacy Experience
function Set-OOBEbypass {
Write-Verbose $LogBreakMessage
Write-Verbose "Setting OOBE Bypass..."
###---Declare RegKey variables
$RegKey = @{
Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
Name = "EnableFirstLogonAnimation"
Value = 0
PropertyType = "DWORD"
}
if (-not (Test-Path $RegKey.Path)) {
Write-Verbose "$($RegKey.Path) does not exist. Creating path."
New-Item -Path $RegKey.Path -Force
Write-Verbose "$($RegKey.Path) path has been created."
}
New-ItemProperty @RegKey -Force
Write-Verbose "Disable First Logon Animation"
###---Clear and redeclare RegKey variables
$RegKey = @{ }
$RegKey = @{
Path = "HKLM:\Software\Policies\Microsoft\Windows\OOBE"
Name = "DisablePrivacyExperience"
Value = 1
PropertyType = "DWORD"
}
if (-not (Test-Path $RegKey.Path)) {
Write-Verbose "$($RegKey.Path) does not exist. Creating path."
New-Item -Path $RegKey.Path -Force
Write-Verbose "$($RegKey.Path) path has been created."
}
New-ItemProperty @RegKey -Force
Write-Verbose "Disable Privacy Experience"
}
function Test-LocalUserExists {
param($LoanerUser)
$user = Get-LocalUser -Name $LoanerUser -ErrorAction SilentlyContinue
return $null -ne $user
}
function RunForestRun {
Write-Verbose "Computer : $ComputerLogName"
Write-Verbose "Log File : $LogDir"
Write-Verbose "Time/Date : $FancyDate"
Write-Verbose $LogBreakMessage
Write-Verbose "Initialization in Progress..."
Write-Verbose "System configuration underway..."
Write-Verbose "The user will be locked out of the loaner on $LoanerUseByDate"
Write-Verbose $LogBreakMessage
Write-Verbose "Program Check"
Write-Verbose "Checking for installed software..."
$softwareList = @(
@{ Name = "Java"; Vendor = "Oracle Corporation" },
@{ Name = "FileWave Client"; Vendor = "FileWave" },
@{ Name = "Windows Agent"; Vendor = "N-able Technologies" }
)
foreach ($software in $softwareList) {
SoftwareInstalled -softwareName $software.Name -vendorName $software.Vendor
}
ProgramCheck
Write-Verbose $LogBreakMessage
Write-Verbose "Setting up loaner user..."
if (Test-LocalUserExists -LoanerUser $LoanerUser) {
Remove_LoanerUser
LoanerUser_Make
}
else {
LoanerUser_Make
}
Set-OOBEbypass
Write-Verbose "Restarting computer"
shutdown /r /t 20 /c "Restarting computer to complete setup"
}
try {
RunForestRun
}
catch {
Write-Verbose "An error occurred: $_" -level "ERROR"
}