我拼凑了一个Powershell脚本,该脚本告诉我列表中每台计算机上都安装了哪些Office版本,但是我只希望它输出缺少16.0基本密钥的那些版本,因此我知道哪些仍需要它。我该如何调整代码来做到这一点?
set-location -Path \\main\
Get-Content PClist.txt |
ForEach-Object {
Write-Output "$_"
$reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $_)
$reg.OpenSubKey('software\Microsoft\Office').GetSubKeyNames() |% {$_}
}|
Out-file \\main\officeinstalls.txt
答案 0 :(得分:0)
我们需要执行的操作使用条件过滤器过滤掉不需要的内容(或找到所需的内容)。您可以执行类似将输出保存到数组或对象中并对其进行迭代以仅获取所需内容的操作。
$myArray = @(12,13,14,15,16,17,18,19,...);
for($i=0;$i -eq $myArray.Count;$i++){
if($myArray.key[$i] -eq 16){
#Output my data
}
}
答案 1 :(得分:0)
如果只希望它输出缺少16.0密钥的内容,以检查Office 2016的安装,则可以执行以下操作。如果计算机是64位,这还将检查32位安装。
$result = Get-Content PClist.txt | ForEach-Object {
if (!(Test-Connection -ComputerName $_ -Count 1 -Quiet)) {
Write-Warning "Could not connect with computer '$_'"
# skip this computer and carry on with the next one
continue
}
$baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $_)
try {
$key = $baseKey.OpenSubKey('SOFTWARE\Microsoft\Office\16.0')
if (!$key) {
# if this is a 64-bit machine, test for the 32-bit version of Office 2016
$key = $baseKey.OpenSubKey('SOFTWARE\Wow6432Node\Microsoft\Office\16.0')
}
# output the computer name if the '16.0' key was not found
if (!$key) { $_ }
}
catch {}
finally {
if ($key) { $key.Close() }
if ($baseKey) { $baseKey.Close() }
}
}
$result | Out-File -FilePath '\\main\NeedToInstallOffice2016.txt' -Force