现在,假设有一个.msu文件和我的本地计算机,是否有一种方法可以遍历这些规则并找出哪个规则未被满足,以及是哪个规则?我能否使用WSUS 3.0类库来实现此目的?或者是否有工具/脚本可用?您可以通过WSUS 3.0类库
更新适用性规则,但它不提供检查规则是否通过的功能,除非(我猜)您运行安装程序,但这并不能告诉您哪个规则失败了。Simon提到WUAPI库不公开内部规则,(据我所知)没有办法将WUAPI ResultCodes与失败的适用性规则匹配。不幸的是,像Microsoft.Deployment.WindowsInstaller.dll这样的库不能与MSU文件一起使用,因此我们没有“现成”的选择。因此,您必须使用代码和(msu.xml)XML文件手动完成。
<Updates>
<UpdateIdentity UpdateID="E6CF1350-C01B-414D-A61F-263D14D133B4" RevisionNumber="1" />
<Properties UpdateType="Category" />
<ApplicabilityRules>
<IsInstalled>
<True />
</IsInstalled>
</ApplicabilityRules>
<UpdateIdentity UpdateID="2bf7ed9c-6f43-493a-b156-db20f08c44c4" RevisionNumber="101" />
<Properties UpdateType="Detectoid" />
<Relationships />
<ApplicabilityRules>
<IsInstalled>
<b.RegSz Key="HKEY_LOCAL_MACHINE" Subkey="SYSTEM\CurrentControlSet\Control\Nls\Language" Value="InstallLanguage" Comparison="EqualTo" Data="0409" />
</IsInstalled>
</ApplicabilityRules>
<UpdateIdentity UpdateID="6AECE9A4-19E3-4BC7-A20C-070A5E31AFF4" RevisionNumber="100" />
<Properties UpdateType="Detectoid" />
<Relationships></Relationships>
<UpdateIdentity UpdateID="3B4B8621-726E-43A6-B43B-37D07EC7019F" />
<ApplicabilityRules>
<IsInstalled>
<b.WmiQuery Namespace="root\cimv2" WqlQuery="SELECT Manufacturer FROM Win32_ComputerSystem WHERE Manufacturer = 'Dell Inc.' or Manufacturer = 'Samsung Electronics' or Manufacturer = 'Hewlett-Packard' or Manufacturer = 'Gateway'" />
</IsInstalled>
</ApplicabilityRules>
</Updates>
使用此代码查看哪些适用规则失败:
private void btnWillPassApplicabilityRules_Click(object sender, EventArgs e)
{
XDocument doc = XDocument.Load("msu.xml");
var elements = doc.Element("Updates").Elements("ApplicabilityRules").Elements("IsInstalled").Elements();
foreach (var element in elements) {
if (element.ToString().StartsWith("<b.RegSz")) {
string subKeyName = element.Attribute("Subkey").Value;
string keyName = element.Attribute("Value").Value;
string keyValue = element.Attribute("Data").Value;
if (!ValueExistsInRegistry(Registry.LocalMachine, subKeyName, keyName, keyValue)) {
Console.WriteLine("Install is not applicable as Applicability Rule failed: " + element.ToString());
}
}
else if (element.ToString().StartsWith("<b.WmiQuery")) {
string nameSpace = element.Attribute("Namespace").Value;
string wqlQuery = element.Attribute("WqlQuery").Value;
if (!ValueExistsInWMI(nameSpace, wqlQuery)) {
Console.WriteLine("Install is not applicable as Applicability Rule failed: " + element.ToString());
}
}
}
}
private bool ValueExistsInRegistry(RegistryKey root, string subKeyName, string keyName, string keyValue)
{
using (RegistryKey key = root.OpenSubKey(subKeyName)) {
if (key != null) return keyValue == key.GetValue(keyName).ToString();
}
return false;
}
private bool ValueExistsInWMI(string nameSpace, string wqlQuery)
{
ManagementScope scope = new ManagementScope(String.Format("\\\\{0}\\" + nameSpace, "."), null);
scope.Connect();
ObjectQuery query = new ObjectQuery(wqlQuery);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
if (searcher.Get().Count == 0) {
return false;
}
else {
return true;
}
return false;
}
}
在运行适用性规则之前,最好先检查更新是否能通过操作系统(OS)和服务包(SP)适用性测试。如果不适用于OS和SP,则没有必要检查注册表、WMI等来确定升级是否能通过规则。
要查看适用性信息,请运行expand
命令行实用程序:
expand -f:* "C:\temp\msu\Windows6.1-KB2973201-x64.msu" "C:\temp\msu"
此将创建以下文件:
- WSUSSCAN.cab
- Windows6.1-KB2973201-x64.cab
- Windows6.1-KB2973201-x64.xml
- Windows6.1-KB2973201-x64-pkgProperties.txt
xml和txt文件需要约5秒钟才能创建。 打开pkgProperties.txt文件并查看顶部行的信息:
ApplicabilityInfo="Windows 7.0 Client SP1;Windows 7.0 Server Core
SP1;Windows 7.0 Embedded SP1;Windows 7.0 Server SP1;Windows 7.0 WinPE
3.1;"
MSDN参考:
Windows中Windows Update独立安装程序的说明。