Powershell 在某些远程计算机上更改表格输出的函数

Powershell 在某些远程计算机上更改表格输出的函数,powershell,powershell-remoting,Powershell,Powershell Remoting,我有一个函数,我正在使用foreach语句块对多台机器运行: function Get-InstalledApps ($appStr) { $appWC = "*$appStr*" if ([IntPtr]::Size -eq 4) { $regpath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' } else { $regpath = @( 'HKLM:\Software\Mic

我有一个函数,我正在使用foreach语句块对多台机器运行:

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)}
}
}
在当地,情况如下:

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}
但在某些计算机上远程运行时

(即:)

调用命令-ComputerName$computer-ScriptBlock ${function:Get-InstalledApps}-ArgumentList$appStr

我得到了上述信息,但在其他人身上我得到了以下信息:

    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   
在没有机会验证某些计算机的PowerShell版本的情况下,我猜第二组结果可能是针对运行<3.0版的计算机运行的结果

有没有办法强制输出在所有计算机上显示为表格(第一个示例输出)

我猜第二组结果可能是针对运行<3.0版的计算机运行的结果

如果您在至少不是版本3的系统上运行该命令,那么您的
[pscustomobject]
强制转换将失败,因为它是在v3中引入的。我本以为这只是触发了一个错误,但它似乎返回了哈希表。一个兼容的解决方案是使用
新对象

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)
}
谢谢你,马特

这很有效,这是我喜欢的方法

如果未安装应用程序或主机处于脱机状态,则if语句的两种变体似乎无法在脚本的另一个点获取输出(仅在安装时显示),并返回为空行,但这似乎是由语句块获取的:

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) { $_ } }} 
$getapps | where {$_.DisplayName -like $appWC} | Select @{n='Computer';e={$env:COMPUTERNAME + "." + $env:USERDNSDOMAIN}},Displayname,Publisher,DisplayVersion,InstallDate,UninstallString
}