Collecting RSOP data in C#

M

mcw.willart

Hi,

I'm trying to find out how to collect RSOP data in C#, but I haven't
been successfull yet.

Below is the code I Use in VBScript:

------------------------------------------------------------------------------------------------------------------------------
Const FL_FORCE_CREATE_NAMESPACE = 4

strComputer = "."

Set locator = CreateObject("WbemScripting.SWbemLocator")

Set connection = locator.ConnectServer _
(strComputer, "root\rsop", null, null, null, null, 0, null)

Set provider = connection.Get("RsopLoggingModeProvider")

provider.RsopCreateSession _
FL_FORCE_CREATE_NAMESPACE, Null, namespaceLocation, hResult, eInfo

Set rsopProv = locator.ConnectServer _
(strComputer, namespaceLocation & "\User", null, null, Null, Null, 0 ,
Null)

Set colItems = rsopProv.ExecQuery("Select * from RSOP_GPO")

For Each objItem in colItems
WScript.Echo objItem.Name
Next

provider.RsopDeleteSession namespaceLocation, hResult
------------------------------------------------------------------------------------------------------------------------------

Are there any examples on how to do this in C#?

Thanks,

Mario
The Netherlands
 
N

Nicholas Paldino [.NET/C# MVP]

Why not just set a reference to the WbemScripting.SWbemLocator COM
component in VS.NET and then run the same code?

You still have to have the COM component installed on whatever machine
you go to, as well as the interop wrapper that is created.

On top of that, you will probably have to pass DBNull for the "null"
values that you are using in VBScript now (Null was analagous to a database
null, while null in C# is analagous to "Nothing" in VB).

Hope this helps.
 
W

Willy Denoyette [MVP]

Hi,

I'm trying to find out how to collect RSOP data in C#, but I haven't
been successfull yet.

Below is the code I Use in VBScript:

------------------------------------------------------------------------------------------------------------------------------
Const FL_FORCE_CREATE_NAMESPACE = 4

strComputer = "."

Set locator = CreateObject("WbemScripting.SWbemLocator")

Set connection = locator.ConnectServer _
(strComputer, "root\rsop", null, null, null, null, 0, null)

Set provider = connection.Get("RsopLoggingModeProvider")

provider.RsopCreateSession _
FL_FORCE_CREATE_NAMESPACE, Null, namespaceLocation, hResult, eInfo

Set rsopProv = locator.ConnectServer _
(strComputer, namespaceLocation & "\User", null, null, Null, Null, 0 ,
Null)

Set colItems = rsopProv.ExecQuery("Select * from RSOP_GPO")

For Each objItem in colItems
WScript.Echo objItem.Name
Next

provider.RsopDeleteSession namespaceLocation, hResult
------------------------------------------------------------------------------------------------------------------------------

Are there any examples on how to do this in C#?

Thanks,

Mario
The Netherlands



You'll have to use System.Management classes to access RSOP.
To give you an idea, following shows you how to create a session....

private ManagementBaseObject inputArgs = null;
private ManagementBaseObject outParams = null;
....

using(ManagementClass mc = new ManagementClass(new ManagementPath(
"root\\rsop:RsopLoggingModeProvider"),
new ObjectGetOptions(null, System.TimeSpan.MaxValue, true)))
{
inputArgs = mc.GetMethodParameters("RsopCreateSession");
inputArgs["flags"] = null;
inputArgs["UserSid"] = null;
outParams = mc.InvokeMethod("RsopCreateSession", inputArgs, null);
if(((uint)outParams.Properties["hResult"].Value) == 0)
Console.WriteLine(outParams.Properties["hResult"].Value);
....
// bind to the namespace and Delete the session when done...
.....


Willy.
 
Top