簡體   English   中英

轉換JScript WMI C#

[英]Converting JScript WMI C#

我正在嘗試使用WMI和System.Management庫將以下代碼轉換為C#。 誰能為我指出正確的方向或提供幫助。

var IMPORT_CHILDREN = 0; // Recursively imports the subkeys of the specified key. 
var IMPORT_INHERITED = 1; // Imports the inherited properties of the keys.  
var IMPORT_NODE_ONLY = 2; // Does not import subkeys from the specified file. 
var IMPORT_MERGE = 4; // Merges the imported keys into the existing configuration instead of completely replacing what is there. 

var strPassword = "ExportingPassw0rd"; 
var strFilePath = "C:\\exported.xml"; 
var strSourceMetabasePath = "/lm/logging/custom logging"; // As represented in the metabase.xml file. 
var strDestinationMetabasePath = "/lm/logging/custom logging"; // Can be different from the source. 
var intFlags = IMPORT_NODE_ONLY | IMPORT_INHERITED; // Import only the node with inherited properties. 

// Make connections to WMI, to the IIS namespace on MyMachine, and to the IIsComputer object. 
var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator"); 
var providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2"); 
var computerObj = providerObj.get("IIsComputer='LM'"); 

// Call export method from the computer object. 
computerObj.Import(strPassword, strFilePath, strSourceMetabasePath, strDestinationMetabasePath, intFlags); 

// Print results. 
WScript.Echo("Imported the node in " + strFilePath + " to " + strDestinationMetabasePath);

.net 4.0:

    dynamic locatorObj = Activator.CreateInstance(Type.GetTypeFromProgID(("WbemScripting.SWbemLocator")));
    dynamic providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2");  
    dynamic computerObj = providerObj.get("IIsComputer='LM'");

    computerObj.Import(strPassword, strFilePath, strSourceMetabasePath, strDestinationMetabasePath, intFlags);  

.net 3.5及以下版本:

    var locatorObj = Activator.CreateInstance(Type.GetTypeFromProgID(("WbemScripting.SWbemLocator")));
    var providerObj = locatorObj._I("ConnectServer", "MyMachine", "root/MicrosoftIISv2");  
    var computerObj = providerObj._I("get", "IIsComputer='LM'");

    computerObj._I("Import", strPassword, strFilePath, strSourceMetabasePath, strDestinationMetabasePath, intFlags);  


public static class ReflectionHlp
{
  public static object _I(this object item, string name, params object[] args)
  {
    if (item == null)
      return null;
    return item.GetType().InvokeMember(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, item, args);
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM