Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ManagementObject to access IIS

Status
Not open for further replies.

ChainsawJoe

Programmer
Dec 5, 2000
154
GB
Hi all,

I have written the code below, ideally with the end plan to create virtual directories with redirects from code, but I can't even get as far as loading the properties; the line [tt]if (obj.Properties.Count == 0)[/tt] errors with "invalid namespace".

I call the function with from a console app and am using the namespacepath value that seems to be the standard one for accessing the IIS managementobject, but I'm now stuck. Does anyone have any ideas please?

Project is called "IISVirtualDirAdmin"

Main App "Program.cs":
[tt]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IISVirtualDirAdmin.Classes;

namespace IISVirtualDirAdmin
{
class Program
{
const string serverName = "WS-01G-033JM";
const string wmiPathToDefaultWebsite = "IIsWebVirtualDirSetting='W3SVC/1/ROOT'";
const string redirectValue =
"*; /google/; +
"; /bbc/; +
", EXACT_DESTINATION";

static void Main(string[] args)
{
WMITest.CreateVirtualDirs(serverName, wmiPathToDefaultWebsite, redirectValue);
}
}
}
[/tt]

Class "Classes\WMI.cs":
[tt]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace IISVirtualDirAdmin.Classes
{
class WMITest
{
public static void CreateVirtualDirs(string serverName, string wmiPathToDefaultWebsite, string redirectValue)
{
try
{
ConnectionOptions options = new ConnectionOptions();
options.Authentication = AuthenticationLevel.PacketPrivacy;

ManagementPath path = new ManagementPath();
path.Server = serverName;
path.NamespacePath = @"root\MicrosoftIISv2";

ManagementScope scope = new ManagementScope(path, options);

using (ManagementObject obj = new ManagementObject(
scope,
new ManagementPath(wmiPathToDefaultWebsite), null))
{
Console.WriteLine("{0}", obj.Path.RelativePath);
if (obj.Properties.Count == 0)
Console.WriteLine("No properties found");

obj.SetPropertyValue("HttpRedirect", redirectValue);
obj.Put();

string httpRedirectValue = obj.GetPropertyValue("HttpRedirect").ToString();
Console.WriteLine("HttpRedirect='{0}'", httpRedirectValue);
}
}
catch (Exception e)
{
Console.WriteLine("ex: " + e.Message.ToString());
}
Console.ReadLine();
}
}
}
[/tt]

Any pointers are greatly appreciated.

CJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top