我在试图理解我的代码背后的逻辑以使其正常工作时遇到了麻烦。只是我得到的一个快速脚本,用于从远程系统检索软件版本并将输出转换为[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]
,但不确定如何去做。有人对如何做到这一点有任何想法或更好的建议吗?