Freigeben über


Exporting and Importing Metabase Nodes Using WMI

Server administrators often need to take a portion of an IIS configuration and duplicate it on another box. Before IIS 6.0, this had to be done manually, or the whole configuration had to be duplicated. Now, two methods of the IIsComputer (WMI) object allow you to save pieces of an IIS configuration to a file and import them to another IIS 6.0 Web server.

IIS 5.1 and earlier: The IIS WMI provider is not available.

Before you try the example code below, read about the IIsComputer.Export (WMI) and IIsComputer.Import (WMI) methods. Replace MyMachine with the name of an IIS 6.0 server and verify the other variables.

Const EXPORT_CHILDREN = 0 ' Adds properties of child keys to the export file. 
Const EXPORT_INHERITED = 1 ' Adds inherited properties of the exported keys to the export file.  
Const EXPORT_NODE_ONLY = 2 ' Does not add subkeys of the specified key to the export file. 
    
Dim strPassword, strFilePath, strMetabasePath, intFlags 
strPassword = "ExportingPassw0rd"  ' Use the same password to import the configuration. 
strFilePath = "C:\exported.xml" 
strMetabasePath = "/lm/logging/custom logging" ' As represented in the metabase.xml file. 
intFlags = EXPORT_NODE_ONLY OR EXPORT_INHERITED ' Show only the node with inherited properties. 
    
' Make connections to WMI, to the IIS namespace on MyMachine, and to the IIsComputer object. 
set locatorObj = CreateObject("WbemScripting.SWbemLocator") 
set providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2") 
Set computerObj = providerObj.get("IIsComputer='LM'") 
    
' Call export method from the computer object. 
computerObj.Export strPassword, strFilePath, strMetabasePath, intFlags 
    
' Print results. 
WScript.Echo "Exported the node at " & strMetabasePath & " to " & strFilePath
var EXPORT_CHILDREN = 0; // Adds properties of child keys to the export file. 
var EXPORT_INHERITED = 1; // Adds inherited properties of the exported keys to the export file.  
var EXPORT_NODE_ONLY = 2; // Does not add subkeys of the specified key to the export file. 
 
var strPassword = "ExportingPassw0rd"; 
var strFilePath = "C:\\exported.xml"; 
var strMetabasePath = "/lm/logging/custom logging"; // As represented in the metabase.xml file. 
var intFlags = EXPORT_NODE_ONLY | EXPORT_INHERITED; // Show 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.Export(strPassword, strFilePath, strMetabasePath, intFlags); 
 
// Print results. 
WScript.Echo("Exported the node at " + strMetabasePath + " to " + strFilePath)

To import the keys that you exported, use the following code. These examples use the custom logging key of the metabase, which had one location, so the exported information must be imported to the same location on the destination server. However, if you exported a virtual directory, you could import it to any site on the destination server.

Const IMPORT_CHILDREN = 0 ' Recursively imports the subkeys of the specified key. 
Const IMPORT_INHERITED = 1 ' Imports the inherited properties of the keys.  
Const IMPORT_NODE_ONLY = 2 ' Does not import subkeys from the specified file. 
Const IMPORT_MERGE = 4 ' Merges the imported keys into the existing configuration instead of completely replacing what is there. 
    
Dim strPassword, strFilePath, strMetabasePath, intFlags 
strPassword = "ExportingPassw0rd" 
strFilePath = "C:\exported.xml" 
strSourceMetabasePath = "/lm/logging/custom logging" ' As represented in the metabase.xml file. 
strDestinationMetabasePath = "/lm/logging/custom logging" ' Can be different from the source. 
intFlags = IMPORT_NODE_ONLY OR IMPORT_INHERITED ' Import only the node with inherited properties. 
    
' Make connections to WMI, to the IIS namespace on MyMachine, and to the IIsComputer object. 
set locatorObj = CreateObject("WbemScripting.SWbemLocator") 
set providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2") 
Set 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
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);