【问题标题】:Functions tabular output changing on some remote computers函数表格输出在某些远程计算机上发生变化
【发布时间】:2017-08-12 12:12:56
【问题描述】:

我有这个函数,我正在使用一个 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}

但是当在某些计算机上远程运行时

(即:)

Invoke-Command -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 版本,我猜测第二组结果可能是针对运行

有什么方法可以强制输出在所有计算机上显示为表格(第一个示例输出)?

【问题讨论】:

    标签: powershell powershell-remoting


    【解决方案1】:

    我猜第二组结果可能是针对运行

    如果您在至少不是版本 3 的系统上运行它,那么您的 [pscustomobject] 转换将失败,因为它是在 v3 中引入的。我本来希望这只会触发一个错误,但它似乎正在返回哈希表。一个兼容的解决方案是改用new-object

    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)
    }
    

    【讨论】:

      【解决方案2】:

      谢谢马特。

      这行得通,这是我的首选方法。

      如果未安装应用程序或主机处于脱机状态,则 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
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-18
        • 1970-01-01
        • 1970-01-01
        • 2017-09-01
        • 2020-09-12
        • 1970-01-01
        • 2010-09-13
        • 2023-03-26
        相关资源
        最近更新 更多