From 494140093330e316047998525a67543de16421ee Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Thu, 5 Feb 2026 02:08:08 -0500 Subject: [PATCH] Update test.ps1 --- test.ps1 | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/test.ps1 b/test.ps1 index b67a38e..51c6ba5 100644 --- a/test.ps1 +++ b/test.ps1 @@ -235,11 +235,16 @@ function Invoke-ServiceImagePathAudit { return $outObj } - $target = "\\$($outObj.ComputerName)\$($outObj.Key)" - $data = $outObj.FixedKey + # Normalize root names so reg.exe remote paths are valid + $regKey = [string]$outObj.Key + $regKey = $regKey -replace '^HKEY_LOCAL_MACHINE', 'HKLM' + $regKey = $regKey -replace '^HKEY_USERS', 'HKU' + + $target = "\\$($outObj.ComputerName)\$regKey" + $data = [string]$outObj.FixedKey if ($ShowProgress) { - Write-Progress -Activity "Repairing ImagePath" -Status "Fixing $($outObj.ComputerName)\$($outObj.Key)" + Write-Progress -Activity "Repairing ImagePath" -Status "Fixing $($outObj.ComputerName)\$regKey" } if ([string]::IsNullOrWhiteSpace($data) -or $data -eq 'N/A' -or $data -eq "Can't Fix") { @@ -247,17 +252,24 @@ function Invoke-ServiceImagePathAudit { return $outObj } + # reg.exe needs embedded quotes escaped, and the whole /d value wrapped in quotes + # Example: "C:\Program Files\App\app.exe" /arg + # becomes: "\"C:\Program Files\App\app.exe\" /arg" + $dataEscaped = $data -replace '"', '\"' + $dataForReg = '"' + $dataEscaped + '"' + if ($PSCmdlet.ShouldProcess($target, "Set ImagePath to: $data")) { try { $args = @( 'ADD', $target, '/v', 'ImagePath', '/t', 'REG_EXPAND_SZ', - '/d', $data, + '/d', $dataForReg, '/f' ) $output = & reg.exe @args 2>&1 + if ($LASTEXITCODE -eq 0) { $outObj.Status = "Fixed" } else { @@ -269,12 +281,14 @@ function Invoke-ServiceImagePathAudit { catch { $outObj.Status = "Failed: $($_.Exception.Message)" } - } else { + } + else { $outObj.Status = "WhatIf" } return $outObj } + } process {