Sdílet prostřednictvím


Code Examples to Configure Metabase Properties

Applies To: Windows Server 2003, Windows Server 2003 with SP1

The following examples demonstrate the syntax for setting a metabase property using different technologies, such as Windows Management Instrumentation (WMI) and Active Directory Service Interfaces (ADSI). The code examples are written in Microsoft® Visual Basic® Scripting Edition (VBScript), JScript®, and Perl.

The examples are simplified to show the essential code that is needed to change a metabase property. If you use these examples, add error checking code and security checking code before using them to configure your server.

Implementation Guidelines

To implement one of the VBScript, JScript, or Perl examples in a command-line administration script

  • For VBScript: Save the script file with the file name extension of .vbs and call a script with "cscript //nologo filename.vbs".

  • For JScript: Save the script file with the file name extension of .js and call a script with "cscript //nologo filename.js".

  • For Perl: Save the script file with the file name extension of .pl and call a script with "perl filename.pl".

To implement one of the VBScript, JScript, or Perl examples in an ASP page

  1. Set security on the Web application to disable anonymous access. Anonymous access to metabase properties is not allowed. The user requesting the ASP page should be an Administrator. In addition, the process executing the ASP page must have permission to modify the metabase.

  2. Add the following lines of ASP code, where the first line is at the top of the file, and "language" is "Microsoft Visual Basic Scripting Edition (VBScript)", "JScript", or "PerlScript":

        <%@ Language="language" %>
        <%
        insert the example code here
        %>
    
  3. Change instances of WScript.Echo to Response.Write, and print to $Response->Write.

  4. Change the file name extension of your file to .asp.

  5. Install ActivePerl with PerlScript to run the Perl examples. ActivePerl can be downloaded from the ActiveState Web site.

Code Examples

In the following examples, there are italicized words that must be replaced by a valid string for the example to execute properly. Replace property_name_1 and property_name_2 with metabase property names. Replace numeric_or_Boolean_value and String_value with a valid data types for that property.

These examples demonstrate how to set a metabase property to a string, number, or Boolean value. These examples do not cover WMI situations where you need to pass in an object.

WMI

Microsoft Visual Basic Scripting Edition (VBScript)

var providerObj=GetObject("winmgmts:/root/MicrosoftIISv2");
var vdirObj=providerObj.get("IIsWebVirtualDirSetting='W3SVC/1/ROOT'");
// Print out the current value of some properties:
WScript.Echo("Before: " + vdirObj.property_name_1 + ", " + vdirObj.property_name_2);
// Set some properties:
vdirObj.property_name_1=numeric_or_Boolean_value;
vdirObj.property_name_2="String_value";
// Save the property changes in the metabase:
vdirObj.Put_();
WScript.Echo("After: " + vdirObj.property_name_1 + ", " + vdirObj.property_name_2);

JScript

var providerObj=GetObject("winmgmts:/root/MicrosoftIISv2");
var vdirObj=providerObj.get("IIsWebVirtualDirSetting='W3SVC/1/ROOT'");
// Print out the current value of some properties:
WScript.Echo("Before: " + vdirObj.property_name_1 + ", " + vdirObj.property_name_2);
// Set some properties:
vdirObj.property_name_1=numeric_or_Boolean_value;
vdirObj.property_name_2="String_value";
// Save the property changes in the metabase:
vdirObj.Put_();
WScript.Echo("After: " + vdirObj.property_name_1 + ", " + vdirObj.property_name_2);

Perl

use Win32;
use Win32::OLE;
my $providerObj=Win32::OLE -> GetObject("winmgmts:/root/MicrosoftIISv2");
my $vdirObj=$providerObj->Get("IIsWebVirtualDirSetting='W3SVC/1'");
# Print out the current value of some properties:
print "Before: ", $vdirObj->{"property_name_1"}, ", ", $vdirObj->{"property_name_2"}, "\n";
# Set some properties:
$vdirObj->{"property_name_1"}=numeric_or_Boolean_value;
$vdirObj->{"property_name_2"}="String_value";
# Save the property changes in the metabase:
vdirObj->Put_();
print "After: ", $vdirObj->{"property_name_1"}, ", ", $vdirObj->{"property_name_2"}, "\n";

ADSI

Microsoft Visual Basic Scripting Edition (VBScript)

set vdirObj=GetObject("IIS://localhost/W3svc/1/Root")
' Print out the current value of some properties:
WScript.Echo "Before: " & vdirObj.property_name_1 & ", " & vdirObj.property_name_2
' Set some properties:
vdirObj.Put "property_name_1", numeric_or_Boolean_value
vdirObj.Put "property_name_2", "String_value"
' Save the property changes in the metabase:
vdirObj.SetInfo
WScript.Echo "After: " & vdirObj.property_name_1 & ", " & vdirObj.property_name_2

JScript

var vdirObj=GetObject("IIS://localhost/W3svc/1/Root");
// Print out the current value of some properties:
WScript.Echo("Before: " + vdirObj.property_name_1 + ", " + vdirObj.property_name_2);
// Set some properties:
vdirObj.Put("property_name_1", numeric_or_Boolean_value);
vdirObj.Put("property_name_2", "String_value");
// Save the property changes in the metabase:
vdirObj.SetInfo();
WScript.Echo("After: " + vdirObj.property_name_1 + ", " + vdirObj.property_name_2);

Perl

use Win32;
use Win32::OLE;
my $vdirObj=Win32::OLE -> GetObject("IIS://localhost/W3SVC/1/Root");
# Print out the current value of some properties:
print "Before: ", $vdirObj->{"property_name_1"}, ", ", $vdirObj->{"property_name_2"}, "\n";
# Set some properties:
$vdirObj->{"property_name_1"}=numeric_or_Boolean_value;
$vdirObj->{"property_name_2"}="String_value";
# Save the property changes in the metabase:
$vdirObj->SetInfo();
print "After: ", $vdirObj->{"property_name_1"}, ", ", $vdirObj->{"property_name_2"}, "\n";

You may encounter errors when running example code that you have altered. The following table lists possible errors and their causes.

Error Code Possible Cause

0x80005006

The property does not exist, or it does not exist at the access location where you are attempting to set it.

For example, you cannot set the AdminServer Metabase Property at the Web site level (W3SVC/1, W3SVC/1/ROOT) or the virtual directory level (W3SVC/1/ROOT/vdir); you can set it only at the World Wide Web Publishing Service (WWW service) level. You cannot set the AdminEmail Metabase Property on any Web, File Transfer Protocol (FTP), or Simple Mail Transfer Protocol (SMTP) metabase key; you can set it only on a Network News Transfer Protocol (NNTP) metabase key.

See the Metabase Property Reference for the metabase property that you are trying to set. Use the Access Locations table to verify that you are accessing the property at a valid location. Or, you can open the MetaBase.xml file on your server and search for the property to see what locations it is set on.

0x8000500C

The data type of the property is different than the data type to which you are attempting to set it.

For example, you cannot set the AspCodepage Metabase Property property to a string ("English"); you can set it only to a number representing a code page (1252).