總的來說,我的目標是獲取遠程計算機列表的VNC版本以及卸載GUID,以便我可以從某些計算機遠程卸載VNC查看器。我使用了Get-WmiObject類的Win32_Product,但是速度非常慢。從PowerShell中刪除列名選擇對象結果
我有以下腳本,但在結果中包含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 ;
我的結果是這樣的:
計算機名 計算機:
名稱 :@ {DisplayName的= VNC查看5.2.3}
DisplayVersion :@ {DisplayVersion = 5.2.3}
ModifyPath :@ {ModifyPath = MSIEXEC。 exe文件/ I {18B1E36F-0DA3-4FDA-BC57-DD815B0DF3B2}}
賣方 :@ {出版商=的RealVNC公司}
我希望它看起來像這樣:
計算機名 計算機:
名稱 :VNC查看器5.2.3
DisplayVersion :5.2.3
ModifyPath :MSIEXEC。EXE/I {18B1E36F-0DA3-4FDA-BC57-DD815B0DF3B2}
供應商 :RealVNC的公司
這是可能的,或者我要對這個腳本完全錯誤?我還沒有想出一種方法來爲多個參數運行此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
嘗試'select-object -ExpandProperty DisplayVersion' – wOxxOm
哇,那簡單,真棒,謝謝你! – pinchepooch