从PowerShell选择对象结果中删除列名称

时间:2016-08-30 15:05:10

标签: powershell uninstall

总体而言,我的目标是获取远程计算机列表的VNC版本以及卸载GUID,以便我可以从某些计算机远程卸载VNC Viewer。我使用了Get-WmiObject -Class Win32_Product,但速度非常慢。

我有以下脚本,但在结果中它包含了select-object参数的名称。

$computers = Get-Content -Path "C:\Computers.txt"

$Results = @()

ForEach ($Computer in $Computers) {
    $Results += New-Object PSObject -Property @{
        "ComputerName" = $Computer

        "Name" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* } `
        | Where-Object -FilterScript {$_.DisplayName -like "VNC V*"} | select-object DisplayName

        "DisplayVersion" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* } `
        | Where-Object -FilterScript {$_.DisplayName -like "VNC V*"} | select-object DisplayVersion

        "ModifyPath" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* } `
        | Where-Object -FilterScript {$_.DisplayName -like "VNC V*"} | select-object ModifyPath

        "Vendor" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* } `
        | Where-Object -FilterScript {$_.DisplayName -like "VNC V*"} | select-object Publisher

    }
}

$Results | Select-Object ComputerName,Name,DisplayVersion,ModifyPath,Vendor | Sort-Object ComputerName  | Export-Csv  C:\VNC.csv -notype ;

我的结果如下:

ComputerName:ComputerName
姓名:@ {DisplayName = VNC查看器5.2.3}
DisplayVersion:@ {DisplayVersion = 5.2.3}
ModifyPath:@ {ModifyPath = MsiExec.exe / I {18B1E36F-0DA3-4FDA-BC57-DD815B0DF3B2}}
供应商:@ {Publisher = RealVNC Ltd}

我希望它看起来像这样:

ComputerName:ComputerName
名称:VNC Viewer 5.2.3
DisplayVersion:5.2.3
ModifyPath:MsiExec.exe / I {18B1E36F-0DA3-4FDA-BC57-DD815B0DF3B2}
供应商:RealVNC Ltd

这是可能的,还是我对这个脚本完全错了?我还没有找到一种方法来为多个参数运行这个Invoke-Command,并且仍然以任何其他方式将结果输出到各个列中。

这个脚本可以正常使用100个计算机:

if (Test-Path C:\VNCInstalled.csv) {Remove-Item C:\VNCInstalled.csv}
if (Test-Path C:\Computers.txt) {Remove-Item C:\Computers.txt}
$DirSearcher = New-Object System.DirectoryServices.DirectorySearcher([adsi]'')
$DirSearcher.Filter = '(&(objectClass=Computer)(!(cn=*esx*)) (!(cn=*slng*)) (!(cn=*dcen*)) )'
$DirSearcher.FindAll().GetEnumerator() | sort-object { $_.Properties.name } `
| ForEach-Object { $_.Properties.name }`
| Out-File -FilePath C:\Computers.txt

Get-Content -Path c:\Computers.txt `
| ForEach-Object {Get-WmiObject -Class Win32_Product -ComputerName $_} `
| Where-Object -FilterScript {$_.Name -like "VNC V*"} `
| select-object @{Name="ComputerName";Expression={$_.PSComputerName}},
                Name,
                @{Name="InstallLocation";Expression={$_.PackageCache}},
                Vendor,
                Version,
                @{Name="GUID";Expression={$_.IdentifyingNumber}} `
| Sort-Object ComputerName `
| Export-CSV -path c:\VNCInstalled.csv -notype

1 个答案:

答案 0 :(得分:1)

将所有Select-Object命令更改为Select-Object -ExpandProperty PropertyName,以丢弃属性名称/列标题。