From 984dadd7591d467796e754e13018c4b35231e272 Mon Sep 17 00:00:00 2001 From: Glenn Sarti Date: Fri, 3 Aug 2018 10:53:27 +0800 Subject: [PATCH] (MODULES-7580) Fix PowerShell task for puppet version on 32bit Previously the powershell based version task was failing on 32bit windows operating systems due to passing in a null path to Test-Path. This commit modifies the detection to be far more tolerant of errors and instead detects if the registry entries are null instead of using Test-Path. This commit also cleans up the code as per PS Script Analyzer rules; * Remove use of aliases * Prefer `$null -eq ...` instead of `... -eq $null` --- tasks/version_powershell.ps1 | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tasks/version_powershell.ps1 b/tasks/version_powershell.ps1 index eedd45a59..03094bf0c 100644 --- a/tasks/version_powershell.ps1 +++ b/tasks/version_powershell.ps1 @@ -1,14 +1,16 @@ -if (Test-Path 'HKLM:\SOFTWARE\Puppet Labs\Puppet') { - $reg = $(Get-ItemProperty -Path 'HKLM:\SOFTWARE\Puppet Labs\Puppet') - if (Test-Path $reg.RememberedInstallDir64) { +$rootPath = 'HKLM:\SOFTWARE\Puppet Labs\Puppet' + +$reg = Get-ItemProperty -Path $rootPath -ErrorAction SilentlyContinue +if ($null -ne $reg) { + if ($null -ne $reg.RememberedInstallDir64) { $loc = $reg.RememberedInstallDir64+'VERSION' - } elseif (Test-Path $reg.RememberedInstallDir) { + } elseif ($null -ne $reg.RememberedInstallDir) { $loc = $reg.RememberedInstallDir+'VERSION' } } -if ($loc -ne $null) { - Write-Output "{`"version`":`"$(type $loc)`",`"source`":`"$($loc.replace('\', '/'))`"}" +if ( ($null -ne $loc) -and (Test-Path -Path $loc) ) { + Write-Output "{`"version`":`"$(Get-Content -Path $loc -ErrorAction Stop)`",`"source`":`"$($loc.replace('\', '/'))`"}" } else { Write-Output '{"version":null,"source":null}' }