ChainsawJoe
Programmer
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
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