-
Notifications
You must be signed in to change notification settings - Fork 20
/
Windows_Path_Enumerate.ps1
567 lines (476 loc) · 25.8 KB
/
Windows_Path_Enumerate.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<#
.SYNOPSIS
Fix for Microsoft Windows Unquoted Service Path Enumeration
.DESCRIPTION
Script for fixing vulnerability "Unquoted Service Path Enumeration" in Services and Uninstall strings. Script modifying registry values.
Require Administrator rights and should be run on x64 powershell version in case if OS also have x64 architecture
.PARAMETER FixServices
This bool parameter allow proceed Services with vulnerability. By default this parameter enabled.
For disabling this parameter use -FixServices:$False
.PARAMETER FixUninstall
Parameter allow find and fix vulnerability in UninstallPaths.
Will be covered paths for x86 and x64 applications on x64 systems.
.PARAMETER FixEnv
Find services with Environment variables in the ImagePath parameter, and replace Env. variable to the it value
EX. %ProgramFiles%\service.exe will be replace to "C:\Program Files\service.exe"
.PARAMETER WhatIf
Parameter should be used for checking possible system impact.
With this parameter script would not change anything on your system,
and only will show information about possible (needed) changes.
.PARAMETER CreateBackup
When switch parameter enabled script will export registry tree`s
specified for services or uninstall strings based on operator selection.
Tree would be exported before any changes.
[Note] For restoring backup could be used RestoreBackup parameter
[Note] For providing full backup path could be used BackupName parameter
.PARAMETER RestoreBackup
This parameter will allow restore previously created backup.
If BackupName parameter would not be provided will be used last created backup,
in other case script will try to find selected backup name
[Note] For creation backup could be used CreateBackup parameter
[Note] For providing full backup path could be used BackupName parameter
.PARAMETER BackupFolderPath
Parameter would be proceeded only with CreateBackup or RestoreBackup
If CreateBackup or RestoreBackup parameter will be provided, then path from this parameter will be used.
During backup will be created reg file with original values per each service and application that will be modified
During restoration all reg files in the specified format will be iterable imported to the registry
Input example: C:\Backup\
Backup file format:
for -FixServices switch => Service_<ServiceName>_YYYY-MM-DD_HHmmss.reg
for -FixUninstall switch => Software_<ApplicationName>_YYYY-MM-DD_HHmmss.reg
.PARAMETER Passthru
With this parameter will be returned object array without any messages in a console
Each element will continue Service\Program Name, Path, Type <Service\Software>, ParamName <ImagePath\UninstallString>, OriginalValue, ExpectedValue
.PARAMETER Silent
[i] Silent parameter will work only together with Passthru parameter
If at least 1 Service Path or Uninstall String should be fixed script will return $true
Otherwise script will return $false
Example:
.\windows_path_enumerate.ps1 -FixUninstall -WhatIf -Passthru -Silent
Output:
$true
Description:
$true mean at least 1 service need to be fixed.
WhatIf switch mean that nothing was fixed, registry was only diagnosed for the vulnerability
.PARAMETER Help
Will display this help message
.PARAMETER LogName
Parameter allow to change output file location, or disable logging setting this parameter to empty string or $null.
.EXAMPLE
# Run powershell as administrator and type path to this script. In case if it will not run type dot (.) before path.
. C:\Scripts\Windows_Path_Enumerate.ps1
VERBOSE:
--------
2017-02-19 15:43:50Z : INFO : ComputerName: W8-NB
2017-02-19 15:43:50Z : Old Value : Service: 'BadDriver' - %ProgramFiles%\bad driver\driver.exe -k -l 'oper'
2017-02-19 15:43:50Z : Expected : Service: 'BadDriver' - "%ProgramFiles%\bad driver\driver.exe" -k -l 'oper'
2017-02-19 15:43:50Z : SUCCESS : New Value of ImagePath was changed for service 'BadDriver'
2017-02-19 15:43:50Z : Old Value : Service: 'NotAVirus' - C:\Program Files\Strange Software\virus.exe -silent
2017-02-19 15:43:51Z : Expected : Service: 'NotAVirus' - "C:\Program Files\Strange Software\virus.exe" -silent'
2017-02-19 15:43:51Z : SUCCESS : New Value of ImagePath was changed for service 'NotAVirus'
Description
-----------
Fix 2 services 'BadDriver', 'NotAVirus'.
Env variable %ProgramFiles% did not changed to full path in service 'BadDriver'
.EXAMPLE
# This command, or similar could be used for running script from SCCM
Powershell -ExecutionPolicy bypass -command ". C:\Scripts\Windows_Path_Enumerate.ps1 -FixEnv"
VERBOSE:
--------
2017-02-19 15:43:50Z : INFO : ComputerName: W8-NB
2017-02-19 15:43:50Z : Old Value : Service: 'BadDriver' - %ProgramFiles%\bad driver\driver.exe -k -l 'oper'
2017-02-19 15:43:50Z : Expected : Service: 'BadDriver' - "C:\Program Files\bad driver\driver.exe" -k -l 'oper'
2017-02-19 15:43:50Z : SUCCESS : New Value of ImagePath was changed for service 'BadDriver'
2017-02-19 15:43:50Z : Old Value : Service: 'NotAVirus' - %SystemDrive%\Strange Software\virus.exe -silent
2017-02-19 15:43:51Z : Expected : Service: 'NotAVirus' - "C:\Strange Software\virus.exe" -silent'
2017-02-19 15:43:51Z : SUCCESS : New Value of ImagePath was changed for service 'NotAVirus'
Description
-----------
Fix 2 services 'BadDriver', 'NotAVirus'.
Env variable %ProgramFiles% replaced to full path 'C:\Program Files' in service 'BadDriver'
.EXAMPLE
# This command, or similar could be used for running script from SCCM
Powershell -ExecutionPolicy bypass -command ". C:\Scripts\Windows_Path_Enumerate.ps1 -FixUninstall -FixServices:$False -WhatIf"
VERBOSE:
--------
2018-07-02 22:23:02Z : INFO : ComputerName: test
2018-07-02 22:23:04Z : Old Value : Software : 'FakeSoft32' - c:\Program files (x86)\Fake inc\Pseudo Software\uninstall.exe -silent
2018-07-02 22:23:04Z : Expected : Software : 'FakeSoft32' - "c:\Program files (x86)\Fake inc\Pseudo Software\uninstall.exe" -silent
Description
-----------
Script will find and displayed
.EXAMPLE
# This command will return $true if at least 1 path should be fixed or $false if there nothing to fix
# Log will not be available
.\windows_path_enumerate.ps1 -FixUninstall -WhatIf -Passthru -Silent -LogName ''
VERBOSE:
--------
true
.NOTES
Name: Windows_Path_Enumerate.PS1
Version: 3.5.1
Author: Vector BCO
Updated: 8 April 2021
.LINK
https://github.com/VectorBCO/windows-path-enumerate/
https://gallery.technet.microsoft.com/scriptcenter/Windows-Unquoted-Service-190f0341
https://www.tenable.com/sc-report-templates/microsoft-windows-unquoted-service-path-enumeration
http://www.commonexploits.com/unquoted-service-paths/
#>
[CmdletBinding(DefaultParameterSetName = "Fixing")]
Param (
[parameter(Mandatory=$false,
ParameterSetName = "Fixing")]
[parameter(Mandatory = $False,
ParameterSetName = "Restoring")]
[Alias("s")]
[Bool]$FixServices=$true,
[parameter(Mandatory = $false,
ParameterSetName = "Fixing")]
[parameter(Mandatory=$False,
ParameterSetName = "Restoring")]
[Alias("u")]
[Switch]$FixUninstall,
[parameter(Mandatory = $false,
ParameterSetName = "Fixing")]
[Alias("e")]
[Switch]$FixEnv,
[parameter(Mandatory = $False,
ParameterSetName = "Fixing")]
[Alias("cb","backup")]
[switch]$CreateBackup,
[parameter(Mandatory=$False,
ParameterSetName = "Restoring")]
[Alias("rb","restore")]
[switch]$RestoreBackup,
[parameter(Mandatory=$False,
ParameterSetName = "Fixing")]
[parameter(Mandatory = $False,
ParameterSetName = "Restoring")]
[string]$BackupFolderPath = "C:\Temp\PathEnumerationBackup",
[parameter(Mandatory = $False,
ParameterSetName = "Fixing")]
[parameter(Mandatory = $False,
ParameterSetName = "Restoring")]
[string]$LogName = "C:\Temp\ServicesFix-3.5.1.Log",
[parameter(Mandatory = $False,
ParameterSetName = "Fixing")]
[parameter(Mandatory = $False,
ParameterSetName = "Restoring")]
[Alias("ShowOnly")]
[Switch]$WhatIf,
[parameter(Mandatory = $False,
ParameterSetName = "Fixing")]
[Switch]$Passthru,
[parameter(Mandatory = $False,
ParameterSetName = "Fixing")]
[Switch]$Silent,
[parameter(Mandatory = $true,
ParameterSetName = "Help")]
[Alias("h")]
[switch]$Help
)
Function Fix-ServicePath {
<#
.SYNOPSIS
Microsoft Windows Unquoted Service Path Enumeration
.DESCRIPTION
Use Fix-ServicePath to fix vulnerability "Unquoted Service Path Enumeration".
.PARAMETER FixServices
This switch parameter allow proceed Services with vulnerability. By default this parameter enabled.
For disable this parameter use -FixServices:$False
.PARAMETER FixUninstall
Parameter allow find and fix vulnerability in UninstallPath.
Will be covered paths for x86 and x64 applications on x64 systems.
.PARAMETER FixEnv
Find services with Environment variables in the ImagePath parameter, and replace Env. variable to the it value
EX. %ProgramFiles%\service.exe will be replace to "C:\Program Files\service.exe"
.PARAMETER WhatIf
Parameter should be used for checking possible system impact.
With this parameter script would not be changing anything on your system,
and only will show information about possible changes
.EXAMPLE
Fix-ServicePath
VERBOSE:
--------
2017-02-19 15:43:50Z : Old Value : Service: 'BadDriver' - %ProgramFiles%\bad driver\driver.exe -k -l 'oper'
2017-02-19 15:43:50Z : Expected : Service: 'BadDriver' - "%ProgramFiles%\bad driver\driver.exe" -k -l 'oper'
2017-02-19 15:43:50Z : SUCCESS : New Value of ImagePath was changed for service 'BadDriver'
2017-02-19 15:43:50Z : Old Value : Service: 'NotAVirus' - C:\Program Files\Strange Software\virus.exe -silent
2017-02-19 15:43:51Z : Expected : Service: 'NotAVirus' - "C:\Program Files\Strange Software\virus.exe" -silent'
2017-02-19 15:43:51Z : SUCCESS : New Value of ImagePath was changed for service 'NotAVirus'
Description
-----------
Fix 2 services 'BadDriver', 'NotAVirus'.
Env variable %ProgramFiles% did not changed to full path in service 'BadDriver'
.EXAMPLE
Fix-ServicePath -FixEnv
VERBOSE:
--------
2017-02-19 15:43:50Z : Old Value : Service: 'BadDriver' - %ProgramFiles%\bad driver\driver.exe -k -l 'oper'
2017-02-19 15:43:50Z : Expected : Service: 'BadDriver' - "C:\Program Files\bad driver\driver.exe" -k -l 'oper'
2017-02-19 15:43:50Z : SUCCESS : New Value of ImagePath was changed for service 'BadDriver'
2017-02-19 15:43:50Z : Old Value : Service: 'NotAVirus' - %SystemDrive%\Strange Software\virus.exe -silent
2017-02-19 15:43:51Z : Expected : Service: 'NotAVirus' - "C:\Strange Software\virus.exe" -silent'
2017-02-19 15:43:51Z : SUCCESS : New Value of ImagePath was changed for service 'NotAVirus'
Description
-----------
Fix 2 services 'BadDriver', 'NotAVirus'.
Env variable %ProgramFiles% replaced to full path 'C:\Program Files' in service 'BadDriver'
.EXAMPLE
Fix-ServicePath -FixUninstall -FixServices:$False -WhatIf
VERBOSE:
--------
2018-07-02 22:23:04Z : Old Value : Software : 'FakeSoft32' - c:\Program files (x86)\Fake inc\Pseudo Software\uninstall.exe -silent
2018-07-02 22:23:04Z : Expected : Software : 'FakeSoft32' - "c:\Program files (x86)\Fake inc\Pseudo Software\uninstall.exe" -silent
Description
-----------
Script will find problems and only display result but will not change anything
.NOTES
Name: Fix-ServicePath
Version: 3.5
Author: Vector BCO
Last Modified: 3 May 2020
.LINK
https://gallery.technet.microsoft.com/scriptcenter/Windows-Unquoted-Service-190f0341
https://www.tenable.com/sc-report-templates/microsoft-windows-unquoted-service-path-enumeration
http://www.commonexploits.com/unquoted-service-paths/
#>
Param (
[bool]$FixServices = $true,
[Switch]$FixUninstall,
[Switch]$FixEnv,
[Switch]$Backup,
[string]$BackupFolder = "C:\Temp\PathEnumeration",
[Switch]$WhatIf,
[Switch]$Passthru
)
# Get all services
$FixParameters = @()
If ($FixServices) {
$FixParameters += @{"Path" = "HKLM:\SYSTEM\CurrentControlSet\Services\" ; "ParamName" = "ImagePath"}
}
If ($FixUninstall) {
$FixParameters += @{"Path" = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" ; "ParamName" = "UninstallString"}
# If OS x64 - adding paths for x86 programs
If (Test-Path "$($env:SystemDrive)\Program Files (x86)\") {
$FixParameters += @{"Path" = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" ; "ParamName" = "UninstallString"}
}
}
If ($Backup){
If (! (Test-Path $BackupFolder)){
New-Item $BackupFolder -Force -ItemType Directory | Out-Null
}
}
$PTElements = @()
ForEach ($FixParameter in $FixParameters) {
Get-ChildItem $FixParameter.Path -ErrorAction SilentlyContinue | ForEach-Object {
$SpCharREGEX = '([\[\]])'
$RegistryPath = $_.name -Replace 'HKEY_LOCAL_MACHINE', 'HKLM:' -replace $SpCharREGEX, '`$1'
$OriginalPath = (Get-ItemProperty "$RegistryPath")
$ImagePath = $OriginalPath.$($FixParameter.ParamName)
If ($FixEnv) {
If ($($OriginalPath.$($FixParameter.ParamName)) -match '%(?''envVar''[^%]+)%') {
$EnvVar = $Matches['envVar']
$FullVar = (Get-ChildItem env: | Where-Object {$_.Name -eq $EnvVar}).value
$ImagePath = $OriginalPath.$($FixParameter.ParamName) -replace "%$EnvVar%", $FullVar
Clear-Variable Matches
} # End If
} # End If $fixEnv
# Get all services with vulnerability
If (($ImagePath -like "* *") -and ($ImagePath -notLike '"*"*') -and ($ImagePath -like '*.exe*')) {
# Skip MsiExec.exe in uninstall strings
If ((($FixParameter.ParamName -eq 'UninstallString') -and ($ImagePath -NotMatch 'MsiExec(\.exe)?') -and ($ImagePath -Match '^((\w\:)|(%[-\w_()]+%))\\')) -or ($FixParameter.ParamName -eq 'ImagePath')) {
$NewPath = ($ImagePath -split ".exe ")[0]
$key = ($ImagePath -split ".exe ")[1]
$trigger = ($ImagePath -split ".exe ")[2]
$NewValue = ''
# Get service with vulnerability with key in ImagePath
If (-not ($trigger | Measure-Object).count -ge 1) {
If (($NewPath -like "* *") -and ($NewPath -notLike "*.exe")) {
$NewValue = "`"$NewPath.exe`" $key"
} # End If
# Get service with vulnerability with out key in ImagePath
ElseIf (($NewPath -like "* *") -and ($NewPath -like "*.exe")) {
$NewValue = "`"$NewPath`""
} # End ElseIf
If ((-not ([string]::IsNullOrEmpty($NewValue))) -and ($NewPath -like "* *")) {
try {
$soft_service = $(if ($FixParameter.ParamName -Eq 'ImagePath') {'Service'}Else {'Software'})
$OriginalPSPathOptimized = $OriginalPath.PSPath -replace $SpCharREGEX, '`$1'
Write-Output "$(get-date -format u) : Old Value : $soft_service : '$($OriginalPath.PSChildName)' - $($OriginalPath.$($FixParameter.ParamName))"
Write-Output "$(get-date -format u) : Expected : $soft_service : '$($OriginalPath.PSChildName)' - $NewValue"
if ($Passthru){
$PTElements += '' | Select-Object `
@{n = 'Name'; e = {$OriginalPath.PSChildName}}, `
@{n = 'Type'; e = {$soft_service}}, `
@{n = 'ParamName'; e = {$FixParameter.ParamName}}, `
@{n = 'Path'; e = {$OriginalPSPathOptimized}}, `
@{n = 'OriginalValue'; e = {$OriginalPath.$($FixParameter.ParamName)}}, `
@{n = 'ExpectedValue'; e = {$NewValue}}
}
If ($Backup){
$BcpFileName = "$BackupFolder\$soft_service`_$($OriginalPath.PSChildName)`_$(get-date -uFormat "%Y-%m-%d_%H%M%S").reg"
$BcpTmpFileName = "$BackupFolder\$soft_service`_$($OriginalPath.PSChildName)`_$(get-date -uFormat "%Y-%m-%d_%H%M%S").tmp"
$BcpRegistryPath = $RegistryPath -replace '\:'
Write-Output "$(get-date -format u) : Creating registry backup : $BcpFileName"
$ExportResult = REG EXPORT $BcpRegistryPath $BcpTmpFileName | Out-String
Get-Content $BcpTmpFileName | Out-File $BcpFileName -Append
Remove-Item $BcpTmpFileName -Force -ErrorAction "SilentlyContinue"
Write-Output "$(get-date -format u) : Backup Result : $($ExportResult -split '\r\n' | Where-Object {$_ -NotMatch '^$'})"
}
If (! $WhatIf) {
Set-ItemProperty -Path $OriginalPSPathOptimized -Name $($FixParameter.ParamName) -Value $NewValue -ErrorAction Stop
$DisplayName = ''
$keyTmp = (Get-ItemProperty -Path $OriginalPSPathOptimized)
If ($soft_service -match 'Software') {
$DisplayName = $keyTmp.DisplayName
}
If ($keyTmp.$($FixParameter.ParamName) -eq $NewValue) {
Write-Output "$(get-date -format u) : SUCCESS : Path value was changed for $soft_service '$($OriginalPath.PSChildName)' $(if($DisplayName){"($DisplayName)"})"
} # End If
Else {
Write-Output "$(get-date -format u) : ERROR : Something is going wrong. Path was not changed for $soft_service '$(if($DisplayName){$DisplayName}else{$OriginalPath.PSChildName})'."
} # End Else
} # End If
} # End try
Catch {
Write-Output "$(get-date -format u) : ERROR : Something is going wrong. Value changing failed in service '$($OriginalPath.PSChildName)'."
Write-Output "$(get-date -format u) : ERROR : $_"
} # End Catch
Clear-Variable NewValue
} # End If
} # End Main If
} # End if (Skip not needed strings)
} # End If
If (($trigger | Measure-Object).count -ge 1) {
Write-Output "$(get-date -format u) : ERROR : Can't parse $($OriginalPath.$($FixParameter.ParamName)) in registry $($OriginalPath.PSPath -replace 'Microsoft\.PowerShell\.Core\\Registry\:\:') "
} # End If
} # End Foreach
} # End Foreach
if ($Passthru){
return $PTElements
}
}
Function Get-OSandPoShArchitecture {
# Check OS architecture
if ((Get-CimInstance Win32_OperatingSystem | Select-Object OSArchitecture).OSArchitecture -match "64.?bits?") {
if ([intptr]::Size -eq 8){
Return $true, $true
} else {
Return $true, $false
}
} else { Return $false, $false }
}
Function Tee-Log {
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
$Input,
[Parameter(Mandatory = $true)]
$FilePath,
[switch]$Silent
)
if($Silent){
$Input | Out-File -FilePath $LogName -Append
} else {
$Input | Tee-Object -FilePath $LogName -Append
}
}
if ((! $FixServices) -and (! $FixUninstall)){
Throw "Should be selected at least one of two parameters: FixServices or FixUninstall. `r`n For more details use 'get-help Windows_Path_Enumerate.ps1 -full'"
}
if ($Help){
Write-Output "For help use this command in powershell: Get-Help $($MyInvocation.MyCommand.Path) -full"
powershell -command "& Get-Help $($MyInvocation.MyCommand.Path) -full"
exit
}
$OS, $PoSh = Get-OSandPoShArchitecture
If (($OS -eq $true) -and ($PoSh -eq $true)){
$validation = "$(get-date -format u) : INFO : Executed x64 Powershell on x64 OS"
} elseIf (($OS -eq $true) -and ($PoSh -eq $false)) {
$validation = "$(get-date -format u) : WARNING : !ATTENTION! : Executed x32 Powershell on x64 OS. Not all vulnerabilities could be fixed.`r`n"
$validation += "$(get-date -format u) : WARNING : For fixing all vulnerabilities should be used x64 Powershell."
} else {
$validation = "$(get-date -format u) : INFO : Executed x32 Powershell on x32 OS"
}
$DeleteLogFile = $false
if ([string]::IsNullOrEmpty($LogName)){
# Log will be written to the temp file if file not specified
$DeleteLogFile = $true
$LogName = New-TemporaryFile
}
If (! (Test-Path $LogName)){
# If path does not exists it should be created
try{
$tmpLogPath = $LogName
if ($tmpLogPath -NotMatch '[\\\/]$') {
$tmpLogName = ($tmpLogPath -split '[\\\/]')[-1]
$tmpLogPath = $tmpLogPath -replace "$tmpLogName`$"
} else {
$tmpLogName = 'ServicesFix-3.X.Log'
}
if (! (Test-Path $tmpLogPath)) {
New-Item -Path $tmpLogPath -Force -ItemType Directory | Out-Null
}
New-Item -Path "$tmpLogPath\$tmpLogName" -Force -ItemType File | Out-Null
$LogName = "$tmpLogPath\$tmpLogName"
} catch {
Throw "Log file '$LogName' does not exists and cannot be created. Error: $_"
}
}
'*********************************************************************' | Tee-Log -FilePath $LogName -Silent:$Passthru
"$(get-date -format u) : INFO : ComputerName: $($Env:ComputerName)" | Tee-Log -FilePath $LogName -Silent:$Passthru
$validation | Tee-Log -FilePath $LogName -Silent:$Passthru
if ($RestoreBackup){
if ($FixServices -and (! $FixUninstall)){
$RegexPart = "Service"
} elseif ($FixUninstall -and (! $FixServices)) {
$RegexPart = "Software"
} elseif ($FixUninstall -and $FixServices) {
$RegexPart = "(Service|Software)"
}
if (Test-Path $BackupFolderPath){
$FilesToImport = Get-ChildItem "$BackupFolderPath\" | Where-Object {$_.Name -match "$RegexPart`_.+_\d{4}-\d{1,2}-\d{1,2}_\d{3,6}\.reg$"}
if ([string]::IsNullOrEmpty($FilesToImport)){
Write-Output "$(get-date -format u) : No backup files find in $BackupFolderPath" | Tee-Log -FilePath $LogName -Silent:$Passthru
} else {
Foreach ($FileToImport in $FilesToImport) {
Write-Output "$(get-date -format u) : Importing '$($FileToImport.Name)' file to the registry" | Tee-Log -FilePath $LogName -Silent:$Passthru
if ($WhatIf){
Write-Output "$(get-date -format u) : Whatif switch selected so nothing changed..." | Tee-Log -FilePath $LogName -Silent:$Passthru
} else {
REGEDIT /s $($FileToImport.FullName)
}
#Write-Output "$(get-date -format u) : Result : $($ImportResult -split '\r\n' | Where-Object {$_ -NotMatch '^$'})" | Tee-Log -FilePath $LogName -Silent:$Passthru
}
}
} else {
Write-Output "$(get-date -format u) : Backup folder does not exists. Nothing to restore..." | Tee-Log -FilePath $LogName -Silent:$Passthru
}
} else {
$ScriptExecutionResult = Fix-ServicePath `
-FixUninstall:$FixUninstall `
-FixServices:$FixServices `
-WhatIf:$WhatIf `
-FixEnv:$FixEnv `
-Passthru:$Passthru `
-Backup:$CreateBackup `
-BackupFolder $BackupFolderPath
if ($Passthru -and (! [string]::IsNullOrEmpty($ScriptExecutionResult))){
$Objects = $ScriptExecutionResult | Where-Object {$_.GetType().Name -eq 'PSCustomObject' }
$ScriptExecutionResult = $ScriptExecutionResult | Where-Object {$_.GetType().Name -ne 'PSCustomObject' }
}
$ScriptExecutionResult | Tee-Log -FilePath $LogName -Silent:$Passthru
If ($Passthru){
If ($Silent -and $(( $Objects | Measure-Object ).Count -ge 1)){
$True
} ElseIf ($Silent){
$False
} Else {
$Objects
}
}
}
if ($DeleteLogFile){
Remove-Item $LogName -Force -ErrorAction "SilentlyContinue"
}