Windows Server > Windows Server Forums > Server Core Developers > PRB - Can not get WMI to work to create a Virtual Directory for IIS.
Ask a questionAsk a question
 

AnswerPRB - Can not get WMI to work to create a Virtual Directory for IIS.

  • Wednesday, October 07, 2009 2:47 PMATS.NET Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    PRB - Can not get WMI to work to create a Virtual Directory for IIS.

    Please help,

    I'm trying to create a virtual directory in IIS using JavaScript. When it comes time to run the "Put_()", it returns an error as "Invalid Object". I am trying this on a Windows 2003 server. Here is the code:

    <script language=javascript>
      var LA;
      var ERR;
     
      var csVIRTUAL_DIRECTORY = "MyVirtualDirectory";
     
      // This path exists with no file I/O restrictions.
     
      var csVIRTUAL_DIRECTORY_PATH = "C:\Temp\Test";
     
      try
      {
        // Make connections to WMI, to the IIS namespace on the given server, and to the Web service.
       
        LA = "access IIS services";
        var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
        LA = "access IIS service providers";
        var objProvider = objLocator.ConnectServer(csWEB_SERVER_ADDRESS, "root/MicrosoftIISv2");
        LA = "access the IIS W3SVC service";
        var objService = objProvider.Get("IIsWebService='W3SVC'");

        // Check to see if the virtual directory already exists.

        LA = "check upon the virtual directory";
        var objCheckVirtualDirectory = null;
       
        try
        {
          objCheckVirtualDirectory = objProvider.Get
          (
            "IIsWebVirtualDir='" + csVIRTUAL_DIRECTORY + "'"
          );
        }
        catch(e)
        {
          objCheckVirtualDirectory = null;
        }

        if (!IsNull(objCheckVirtualDirectory))
        {
          ERR = "The virtual directory " + ts(csVIRTUAL_DIRECTORY) + ", already exists";
          throw ERR;
        }
       
        objCheckVirtualDirectory = null;
       
        // Add a virtual directory to the site. This requires SpawnInstance().
       
        LA = "prepare virtual directory settings";
        var objVirtualDirectorySettings = objProvider.Get("IIsWebVirtualDirSetting");
        var objNewVirtualDirectory = objVirtualDirectorySettings.SpawnInstance_();
       
        if (IsNull(objNewVirtualDirectory))
        {
          throw "Unable to spawn VDM. Ensure you have full admin security privileges";
        }
       
        objNewVirtualDirectory.Name = csVIRTUAL_DIRECTORY;
        objNewVirtualDirectory.Path = csVIRTUAL_DIRECTORY_PATH;
        objNewVirtualDirectory.AuthFlags = 5; // AuthNTLM + AuthAnonymous
        objNewVirtualDirectory.AnonymousUserName = "GOD-ACCOUNT"; // No restrictions...
        objNewVirtualDirectory.AnonymousUserPass = "password"; // Valid password..
        objNewVirtualDirectory.EnableDefaultDoc = true;
        objNewVirtualDirectory.DirBrowseFlags = 0x4000003E; // date, time, size, extension, longdate
        objNewVirtualDirectory.AccessFlags = 513; // read, script

        // Save the new settings to the metabase.
       
        LA = "create the virtual directory";
        objNewVirtualDirectory.Put_(); // Error occurs here...
       
        alert("Success!!!");
      }
      catch(e)
      {
        ERR = "" + e;
        alert("Error! In an attempt to " + LA + ", the following error occurred:\r\n" + ERR + ".");
      }
    </script>

Answers

All Replies