我有几个远程设备,我需要在其中比较使用 powershell 的设备的添加删除程序。我该怎么做。谁能帮我。
例子:-
需要比较“A 计算机”与“A1 计算机”和“B 计算机”与“B2 计算机”的 appremove 程序。
我有几个远程设备,我需要在其中比较使用 powershell 的设备的添加删除程序。我该怎么做。谁能帮我。
例子:-
需要比较“A 计算机”与“A1 计算机”和“B 计算机”与“B2 计算机”的 appremove 程序。
Microsoft Scripting Guy建议检查注册表而不是非常慢的Win32_Product
类。这是他们按名称搜索计算机列表的示例:
$computers = Import-Csv “C:\PowerShell\computerlist.csv”
$array = @()
foreach($pc in $computers){
$computername=$pc.computername
#Define the variable to hold the location of Currently Installed Programs
$UninstallKey=”SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall”
#Create an instance of the Registry Object and open the HKLM base key
$reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey(‘LocalMachine’,$computername)
#Drill down into the Uninstall key using the OpenSubKey Method
$regkey=$reg.OpenSubKey($UninstallKey)
#Retrieve an array of string that contain all the subkey names
$subkeys=$regkey.GetSubKeyNames()
#Open each Subkey and use GetValue Method to return the required values for each
foreach($key in $subkeys){
$thisKey=$UninstallKey+”\\”+$key
$thisSubKey=$reg.OpenSubKey($thisKey)
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name “ComputerName” -Value $computername
$obj | Add-Member -MemberType NoteProperty -Name “DisplayName” -Value $($thisSubKey.GetValue(“DisplayName”))
$obj | Add-Member -MemberType NoteProperty -Name “DisplayVersion” -Value $($thisSubKey.GetValue(“DisplayVersion”))
$obj | Add-Member -MemberType NoteProperty -Name “InstallLocation” -Value $($thisSubKey.GetValue(“InstallLocation”))
$obj | Add-Member -MemberType NoteProperty -Name “Publisher” -Value $($thisSubKey.GetValue(“Publisher”))
$array += $obj
}
}
$array | Where-Object { $_.DisplayName } | select ComputerName, DisplayName, DisplayVersion
我们可以比较对象列表Compare-Object
以查看 B PC 中缺少哪些软件:
$ComputerA = $array | Where-Object { $_.ComputerName -like 'ComputerA' }
$ComputerB = $array | Where-Object { $_.ComputerName -like 'ComputerB' }
Compare-Object $ComputerA $ComputerB -Property DisplayName,DisplayVersion
DisplayName DisplayVersion SideIndicator
----------- -------------- -------------
Notepad++ (64-bit x64) 7.8.9 <=