From e89cf414a3403344ab40dec2120ef14392edcaa8 Mon Sep 17 00:00:00 2001 From: Jaap Brasser Date: Thu, 5 Apr 2018 12:15:46 +0200 Subject: [PATCH] Updated object creation for PowerShell 3 --- Get-RemoteProgram/Get-RemoteProgram.ps1 | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Get-RemoteProgram/Get-RemoteProgram.ps1 b/Get-RemoteProgram/Get-RemoteProgram.ps1 index 56f58f5..0d32cfe 100644 --- a/Get-RemoteProgram/Get-RemoteProgram.ps1 +++ b/Get-RemoteProgram/Get-RemoteProgram.ps1 @@ -79,10 +79,15 @@ Will retrieve the installed programs on server01/02 that are passed on to the fu begin { $RegistryLocation = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\', 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\' - $HashProperty = @{} - $SelectProperty = @('ProgramName','ComputerName') - if ($Property) { - $SelectProperty += $Property + + if ($psversiontable.psversion.major -gt 2) { + $HashProperty = [ordered]@{} + } else { + $HashProperty = @{} + $SelectProperty = @('ComputerName','ProgramName') + if ($Property) { + $SelectProperty += $Property + } } } @@ -95,16 +100,20 @@ Will retrieve the installed programs on server01/02 that are passed on to the fu $CurrentRegKey = $RegBase.OpenSubKey($CurrentReg) if ($CurrentRegKey) { $CurrentRegKey.GetSubKeyNames() | ForEach-Object { + $HashProperty.ComputerName = $Computer + $HashProperty.ProgramName = ($DisplayName = ($RegBase.OpenSubKey("$CurrentReg$_")).GetValue('DisplayName')) if ($Property) { foreach ($CurrentProperty in $Property) { $HashProperty.$CurrentProperty = ($RegBase.OpenSubKey("$CurrentReg$_")).GetValue($CurrentProperty) } } - $HashProperty.ComputerName = $Computer - $HashProperty.ProgramName = ($DisplayName = ($RegBase.OpenSubKey("$CurrentReg$_")).GetValue('DisplayName')) if ($DisplayName) { - New-Object -TypeName PSCustomObject -Property $HashProperty | - Select-Object -Property $SelectProperty + if ($psversiontable.psversion.major -gt 2) { + [pscustomobject]$HashProperty + } else { + New-Object -TypeName PSCustomObject -Property $HashProperty | + Select-Object -Property $SelectProperty + } } } }