Update test.ps1
changed the RepairOne
This commit is contained in:
73
test.ps1
73
test.ps1
@@ -235,16 +235,12 @@ function Invoke-ServiceImagePathAudit {
|
|||||||
return $outObj
|
return $outObj
|
||||||
}
|
}
|
||||||
|
|
||||||
# Normalize root names so reg.exe remote paths are valid
|
$computer = [string]$outObj.ComputerName
|
||||||
$regKey = [string]$outObj.Key
|
$fullKey = [string]$outObj.Key
|
||||||
$regKey = $regKey -replace '^HKEY_LOCAL_MACHINE', 'HKLM'
|
$data = [string]$outObj.FixedKey
|
||||||
$regKey = $regKey -replace '^HKEY_USERS', 'HKU'
|
|
||||||
|
|
||||||
$target = "\\$($outObj.ComputerName)\$regKey"
|
|
||||||
$data = [string]$outObj.FixedKey
|
|
||||||
|
|
||||||
if ($ShowProgress) {
|
if ($ShowProgress) {
|
||||||
Write-Progress -Activity "Repairing ImagePath" -Status "Fixing $($outObj.ComputerName)\$regKey"
|
Write-Progress -Activity "Repairing ImagePath" -Status "Fixing $computer"
|
||||||
}
|
}
|
||||||
|
|
||||||
if ([string]::IsNullOrWhiteSpace($data) -or $data -eq 'N/A' -or $data -eq "Can't Fix") {
|
if ([string]::IsNullOrWhiteSpace($data) -or $data -eq 'N/A' -or $data -eq "Can't Fix") {
|
||||||
@@ -252,34 +248,50 @@ function Invoke-ServiceImagePathAudit {
|
|||||||
return $outObj
|
return $outObj
|
||||||
}
|
}
|
||||||
|
|
||||||
# reg.exe needs embedded quotes escaped, and the whole /d value wrapped in quotes
|
# Determine hive + subkey from the Key string we scanned
|
||||||
# Example: "C:\Program Files\App\app.exe" /arg
|
$hive = $null
|
||||||
# becomes: "\"C:\Program Files\App\app.exe\" /arg"
|
$subKeyPath = $null
|
||||||
$dataEscaped = $data -replace '"', '\"'
|
|
||||||
$dataForReg = '"' + $dataEscaped + '"'
|
|
||||||
|
|
||||||
if ($PSCmdlet.ShouldProcess($target, "Set ImagePath to: $data")) {
|
if ($fullKey -match '^(HKEY_LOCAL_MACHINE|HKLM)\\(?<sub>.+)$') {
|
||||||
|
$hive = [Microsoft.Win32.RegistryHive]::LocalMachine
|
||||||
|
$subKeyPath = $Matches['sub']
|
||||||
|
}
|
||||||
|
elseif ($fullKey -match '^(HKEY_USERS|HKU)\\(?<sub>.+)$') {
|
||||||
|
$hive = [Microsoft.Win32.RegistryHive]::Users
|
||||||
|
$subKeyPath = $Matches['sub']
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$outObj.Status = "Failed: Unsupported registry root in Key: $fullKey"
|
||||||
|
return $outObj
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($PSCmdlet.ShouldProcess("$computer\$fullKey", "Set ImagePath to: $data")) {
|
||||||
try {
|
try {
|
||||||
$args = @(
|
# Open remote HKLM/HKU
|
||||||
'ADD', $target,
|
$base = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($hive, $computer)
|
||||||
'/v', 'ImagePath',
|
|
||||||
'/t', 'REG_EXPAND_SZ',
|
|
||||||
'/d', $dataForReg,
|
|
||||||
'/f'
|
|
||||||
)
|
|
||||||
|
|
||||||
$output = & reg.exe @args 2>&1
|
# Open the service key writable
|
||||||
|
$svcKey = $base.OpenSubKey($subKeyPath, $true)
|
||||||
if ($LASTEXITCODE -eq 0) {
|
if ($null -eq $svcKey) {
|
||||||
$outObj.Status = "Fixed"
|
$outObj.Status = "Failed: Registry key not found: $fullKey"
|
||||||
} else {
|
return $outObj
|
||||||
$msg = ($output | Out-String).Trim()
|
|
||||||
if ([string]::IsNullOrWhiteSpace($msg)) { $msg = "reg.exe exit code $LASTEXITCODE" }
|
|
||||||
$outObj.Status = "Failed: $msg"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
# Write as REG_EXPAND_SZ (ExpandString)
|
||||||
|
$svcKey.SetValue('ImagePath', $data, [Microsoft.Win32.RegistryValueKind]::ExpandString)
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
$svcKey.Close()
|
||||||
|
$base.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
$outObj.Status = "Fixed"
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
$outObj.Status = "Failed: $($_.Exception.Message)"
|
$msg = $_.Exception.Message
|
||||||
|
if ([string]::IsNullOrWhiteSpace($msg)) { $msg = "$_" }
|
||||||
|
$outObj.Status = "Failed: $msg"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -289,6 +301,7 @@ function Invoke-ServiceImagePathAudit {
|
|||||||
return $outObj
|
return $outObj
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process {
|
process {
|
||||||
|
|||||||
Reference in New Issue
Block a user