Search code examples
powershellpowershell-remoting

Functions tabular output changing on some remote computers


I have this function I'm using a foreach statement block to run against a number of machines:

function Get-InstalledApps ($appStr) {
$appWC = "*$appStr*"
if ([IntPtr]::Size -eq 4) {
    $regpath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
}
else {
    $regpath = @(
        'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
        'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
    )
}
$getapps = Get-ItemProperty $regpath | .{process{if($_.DisplayName -and $_.UninstallString) { $_ } }} 
Foreach ($app in $getapps | where {$_.DisplayName -like $appWC}) {
    [pscustomobject]@{Computer = ($env:COMPUTERNAME + "." + $env:USERDNSDOMAIN)
                        AppName = ($app.displayname)
                        Publisher = ($app.Publisher)
                        DisplayVersion = ($app.DisplayVersion)
                        InstallDate = ($app.InstallDate)
                        UninstallString = ($App.UninstallString)}
}
}

Locally, it looks like this:

PS C:\windows\system32> Get-InstalledApps ibm | ft

Computer            AppName                           Publisher DisplayVersion InstallDate UninstallString                                     
--------            -------                           --------- -------------- ----------- ---------------                                     
Computer.domain.COM IBM Tivoli Storage Manager Client IBM       06.04.0001     20140807    MsiExec.exe /I{FF99015E-71B4-41AB-8985-67D99383A72A}

But when run remotely on some computers

(i.e:)

Invoke-Command -ComputerName $computer -ScriptBlock ${function:Get-InstalledApps} -ArgumentList $appStr

I get the above, however on others I get this:

    Name                           Value                                                                                                                                                                                            
----                           -----                                                                                                                                                                                            
UninstallString                MsiExec.exe /I{68C09095-AC00-4541-B46B-0835F2BDB0CE}                                                                                                                                             
Computer                       comp1.domain.com                                                                                                                                                  
Publisher                      IBM                                                                                                                                                                                              
InstallDate                    20150122                                                                                                                                                                                         
DisplayVersion                 07.01.0000                                                                                                                                                                                       
AppName                        IBM Tivoli Storage Manager Client                                                                                                                                                                
UninstallString                MsiExec.exe /X{1316AC9A-7A5D-4866-B41F-4B3CF03CE52A}                                                                                                                                             
Computer                       comp2.domain.com                                                                                                                                                  
Publisher                      IBM Corp.                                                                                                                                                                                        
InstallDate                    20170226                                                                                                                                                                                         
DisplayVersion                 9.2.7.53                                                                                                                                                                                         
AppName                        IBM BigFix Client   

Without having a chance to verify PowerShell versions of some of the computers yet, I'm guessing the 2nd set of results may be as a result of being run against computers running < version 3.0.

Any way to force the output to display as a table (1st example output) on all computers?


Solution

  • I'm guessing the 2nd set of results may be as a result of being run against computers running < version 3.0.

    If you are running that on systems that are not at least version 3 then your [pscustomobject] cast would fail since that was introduced in v3. I would have expected that to just trigger an error but instead it appears to be returning the hashtable. A compatible solution would be to use new-object instead.

    New-Object -TypeName PSCustomObject -Property @{
        Computer = ($env:COMPUTERNAME + "." + $env:USERDNSDOMAIN)
        AppName = ($app.displayname)
        Publisher = ($app.Publisher)
        DisplayVersion = ($app.DisplayVersion)
        InstallDate = ($app.InstallDate)
        UninstallString = ($App.UninstallString)
    }