powershell - 为每个查询的系统添加分隔符?自定义对象
问题描述
我在试图理解我的代码背后的逻辑以使其正常工作时遇到了麻烦。只是我得到的一个快速脚本,用于从远程系统检索软件版本并将输出转换为[PSCustomObject]
.
[System.Collections.ArrayList]$NetBIOSNames = @('ComputerOne','ComputerTwo')
[System.Collections.ArrayList]$softwareNames = @("ActivClient","Adobe Acrobat", "ATHOC", "DSET", "Firefox", "Google Chrome", "Java", "McAfee Agent")
[System.Collections.ArrayList]$VersionNumber = @()
[System.Collections.ArrayList]$CorrectVersion = @('7.2.1','21.001.20138','6.2.27.271','1.6.8','78.8.0','89.0.4389.72','8.0.2710.9','5.7.1.116')
foreach($Computer in $NetBIOSNames){
foreach ($software in $softwareNames) {
$Version = Invoke-Command -ComputerName $Computer -ScriptBlock {
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*, HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-Object {$_.displayname -like "*$Using:software*" }
}
$null = $VersionNumber.Add($($Version.DisplayVersion))
}
For($i=0; $i -lt $softwareNames.Count; $i++){
[PSCustomObject]@{
"Computer Name" = $NetBIOSNames[$i]
"Software Name" = $softwareNames[$i]
"Current Version" = $VersionNumber[$i]
"Correct Version" = $CorrectVersion[$i]
}
}
}
因此,当针对单台计算机运行时,它会返回以下内容,这是所需的输出:
Computer Name Software Name Current Version Correct Version
------------- ------------- --------------- ---------------
ComputerOne ActivClient 7.2.1 7.2.1
Adobe Acrobat 21.001.20138 21.001.20138
ATHOC 6.2.27.271 6.2.27.271
DSET 1.6.8 1.6.8
Firefox 78.8.0 78.8.0
Google Chrome 89.0.4389.72 89.0.4389.72
Java {8.0.2710.9, 8.0.2710.9} 8.0.2710.9
McAfee Agent {5.7.1.116, 5.7.1.116} 5.7.1.116
....但是,当针对多个运行时,它会返回:
Computer Name Software Name Current Version Correct Version
------------- ------------- --------------- ---------------
ComputerOne ActivClient 7.2.1 7.2.1
ComputerTwo Adobe Acrobat 21.001.20138 21.001.20138
ATHOC 6.2.27.271 6.2.27.271
DSET 1.6.8 1.6.8
Firefox 78.8.0 78.8.0
Google Chrome 89.0.4389.72 89.0.4389.72
Java {8.0.2710.9, 8.0.2710.9} 8.0.2710.9
McAfee Agent {5.7.1.116, 5.7.1.116} 5.7.1.116
ComputerOne ActivClient 7.2.1 7.2.1
ComputerTwo Adobe Acrobat 21.001.20138 21.001.20138
ATHOC 6.2.27.271 6.2.27.271
DSET 1.6.8 1.6.8
Firefox 78.8.0 78.8.0
Google Chrome 89.0.4389.72 89.0.4389.72
Java {8.0.2710.9, 8.0.2710.9} 8.0.2710.9
McAfee Agent {5.7.1.116, 5.7.1.116} 5.7.1.116
我想返回的是以下内容:
Computer Name Software Name Current Version Correct Version
------------- ------------- --------------- ---------------
ComputerOne ActivClient 7.2.1 7.2.1
Adobe Acrobat 21.001.20138 21.001.20138
ATHOC 6.2.27.271 6.2.27.271
DSET 1.6.8 1.6.8
Firefox 78.8.0 78.8.0
Google Chrome 89.0.4389.72 89.0.4389.72
Java {8.0.2710.9, 8.0.2710.9} 8.0.2710.9
McAfee Agent {5.7.1.116, 5.7.1.116} 5.7.1.116
ComputerTwo ActivClient 7.2.1 7.2.1
Adobe Acrobat 21.001.20138 21.001.20138
ATHOC 6.2.27.271 6.2.27.271
DSET 1.6.8 1.6.8
Firefox 78.8.0 78.8.0
Google Chrome 89.0.4389.72 89.0.4389.72
Java {8.0.2710.9, 8.0.2710.9} 8.0.2710.9
McAfee Agent {5.7.1.116, 5.7.1.116} 5.7.1.116
请原谅这是一个很长的帖子。只是有点坚持我做得不对。我认为这是我列举的方式[PSCustomObject]
,但不确定如何去做。有人对如何做到这一点有任何想法或更好的建议吗?
解决方案
这是我个人会做的,在远程服务器中创建 PSObject 对我来说很有意义。此外Invoke-Command
,计算机阵列比循环每台计算机要快得多。
我应该提到的一件事是,哈希表键应该是确切的显示名称。
$NetBIOSNames = 'ComputerOne','ComputerTwo' # I always encourage using ArrayList // GenericList but for an array of computers is not much needed.
$scriptBlock={
$regPaths=@(
'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
)
$softwareMap = @{
'ActivClient'='7.2.1'
'Adobe Acrobat'='21.001.20138'
'ATHOC'='6.2.27.271'
'DSET'='1.6.8'
'Firefox'='78.8.0'
'Google Chrome'='89.0.4389.72'
'Java'='8.0.2710.9'
'McAfee Agent'='5.7.1.116'
}
$itemProps = Get-ItemProperty $regPaths | Where-Object {$_.DisplayName -iin $softwareMap.Keys}
foreach($item in $itemProps)
{
$correctVersion = $softwareMap[$item.DisplayName]
[pscustomobject]@{
'Computer Name' = $env:COMPUTERNAME # Definetly reccommend populating this prop for all Objects, easier to filter in Excel :)
'Software Name' = $item.DisplayName
'Current Version' = $item.DisplayVersion -join ', '
'Correct Version' = $correctVersion
'Correct Version (Bool)' = $CorrectVersion -in $item.DisplayVersion # Using -in in case DisplayVersion is an array. Having a Bool for your Excel is also nice to have.
}
}
}
$result = Invoke-Command -ComputerName $NetBIOSNames -ScriptBlock $scriptBlock -HideComputerName
$result[0] #> Here is the PSObject of Computer 1
推荐阅读
- laravel - Laravel 队列事件在页面完全加载之前立即执行
- .net - 对解决方案的任何更改都会产生 CefSharp“找不到文件”错误
- node.js - mongoose suppressWarning 选项
- javascript - 显示输入颜色选择器时无法输入输入文本
- sql - BigQuery,使用 ARRAY_AGG 创建逗号分隔的结果
- asp.net-core - Blazor Webassembly 和 OData(命名空间 Microsoft.Aspnetcore 中不存在 MVC)
- java - Oracle SQL Developer 不会连接到本地数据库,OCI 库不会加载
- graphdb - 不允许使用空 uri
- javascript - 数据在屏幕上呈现,但在控制台上有错误(Firebase with Vue):未捕获(承诺中)TypeError:无法读取 null 的属性“内容”
- vuetify.js - vuetify 数据表在 th 上插入输入