为Out-GridView列添加自定义对象

时间:2018-04-04 12:10:50

标签: powershell object gridview

让我说我有:

$installed_apps = invoke-command -computername P1184CDC -scriptblock {
Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"| ? DisplayName -ne $null
Get-ItemProperty "HKLM:\Software\wow6432node\Microsoft\Windows\CurrentVersion\Uninstall\*" | ? DisplayName -ne $null
}


$installed_apps | Out-GridView -wait

这将在一个漂亮的gridview中返回所有已安装的应用程序(第一个命令为32位,包含wow6432node的命令为64位):

enter image description here

我正在尝试在结果中添加“架构”列,因此我可以识别从命令返回的所有64位对象:

 Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"| ? DisplayName -ne 

并从命令返回所有32位对象:

Get-ItemProperty "HKLM:\Software\wow6432node\Microsoft\Windows\CurrentVersion\Uninstall\*" | ? DisplayName -ne $null

现在他们都在一起但是能够按32位或64位类型对它们进行排序会很好。

我认为我必须使用New-Object PsObject,例如:

$architecture = New-Object PSObject -Property @{ 
Architecture = "x86"
}

在ForEach循环中,但我对如何将它们与命令返回的应用程序一起设置完全不太熟悉。谢谢你的时间!

1 个答案:

答案 0 :(得分:3)

这将为返回的对象添加'Architecture'属性(因此,GridView中的相应列):

$installed_apps = invoke-command  -scriptblock {
    Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | 
        Where-Object DisplayName -ne $null |
            Add-Member -MemberType NoteProperty -Name Architecture -Value "64-bit" -PassThru

    Get-ItemProperty "HKLM:\Software\wow6432node\Microsoft\Windows\CurrentVersion\Uninstall\*" | 
        Where-Object DisplayName -ne $null |
            Add-Member -MemberType NoteProperty -Name Architecture -Value "32-bit" -PassThru
}


$installed_apps | Out-GridView -wait

顺便说一句, wow6432node 节点是32位应用程序读/写的位置,而不是64位。