Problem with ManagementObject..

G

Guest

hi,
I am trying following code.

string fileObject = @"C:\Documents and Settings\myfolder\Temp.txt";
ManagementBaseObject ret = null;
ManagementPath path = new ManagementPath( );
path.Server = @"ServerName"; // Assuming that server name is server i am
trying to connect to

path.NamespacePath = @"root\cimv2";
path.RelativePath = @"Win32_LogicalFileSecuritySetting.Path=" + "'" +
fileObject + "'";
ManagementObject lfs = new ManagementObject(path);

// Get the security descriptor for this object
bool EnablePrivileges = lfs.Scope.Options.EnablePrivileges;
lfs.Scope.Options.EnablePrivileges = true;
// Dump all trustees (this includes owner)
string accountName, SID;
foreach (ManagementBaseObject b in lfs.GetRelated())
{
accountName= (string)b["AccountName"]; --I am getting error on
this line
SID = (string)b["SID"];
Console.WriteLine("Trustees {0} is {1}", b["AccountName"],
b["SID"]);
}

I am getting error on the line marked(--I am getting error on this line) in
above code. When i tried above code for local machine everything is working
fine.
Thanks in advance for help.
 
G

Guest

Sorry, I didnot post the error message.
Error message simply says "Not Found"
and stack trace is

at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)\r\n at System.Management.PropertyData.RefreshPropertyInfo()\r\n
at System.Management.PropertyData..ctor(ManagementBaseObject parent, String
propName)\r\n at System.Management.PropertyDataCollection.get_Item(String
propertyName)\r\n at
System.Management.ManagementBaseObject.GetPropertyValue(String
propertyName)\r\n at System.Management.ManagementBaseObject.get_Item(String
propertyName)\r\n at Tester.testWMI() in
c:\\temp\\ecconsole\\batchconsoleapp\\wmicodesample.cs:line 40" string

--
Thanks
SCS


Willy Denoyette said:
Should work.
Please post the error message or call stack...

Willy.

scsharma said:
hi,
I am trying following code.

string fileObject = @"C:\Documents and Settings\myfolder\Temp.txt";
ManagementBaseObject ret = null;
ManagementPath path = new ManagementPath( );
path.Server = @"ServerName"; // Assuming that server name is server i am
trying to connect to

path.NamespacePath = @"root\cimv2";
path.RelativePath = @"Win32_LogicalFileSecuritySetting.Path=" + "'" +
fileObject + "'";
ManagementObject lfs = new ManagementObject(path);

// Get the security descriptor for this object
bool EnablePrivileges = lfs.Scope.Options.EnablePrivileges;
lfs.Scope.Options.EnablePrivileges = true;
// Dump all trustees (this includes owner)
string accountName, SID;
foreach (ManagementBaseObject b in lfs.GetRelated())
{
accountName= (string)b["AccountName"]; --I am getting error on
this line
SID = (string)b["SID"];
Console.WriteLine("Trustees {0} is {1}", b["AccountName"],
b["SID"]);
}

I am getting error on the line marked(--I am getting error on this line)
in
above code. When i tried above code for local machine everything is
working
fine.
Thanks in advance for help.
 
W

Willy Denoyette [MVP]

scsharma said:
Sorry, I didnot post the error message.
Error message simply says "Not Found"
and stack trace is

at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)\r\n at
System.Management.PropertyData.RefreshPropertyInfo()\r\n
at System.Management.PropertyData..ctor(ManagementBaseObject parent,
String
propName)\r\n at
System.Management.PropertyDataCollection.get_Item(String
propertyName)\r\n at
System.Management.ManagementBaseObject.GetPropertyValue(String
propertyName)\r\n at
System.Management.ManagementBaseObject.get_Item(String
propertyName)\r\n at Tester.testWMI() in
c:\\temp\\ecconsole\\batchconsoleapp\\wmicodesample.cs:line 40" string

This works for me (on XP and higher),

using System;
using System.Management;
using System.Collections;

class Tester {
public static void Main()
{
// Create FileObjectSecurity passing the file object (filepath) to the
ctor.
// Watch the double backslashes !!!!
string FileSystemObject = @"c:\\rssfeed.xml";

ManagementPath path = new ManagementPath( );

path.RelativePath = @"Win32_LogicalFileSecuritySetting.Path=" + "'" +
FileSystemObject + "'";
ManagementObject lfs = new ManagementObject(path);
foreach (ManagementBaseObject b in lfs.GetRelated())
{
string domainName= b["ReferencedDomainName"].ToString();
string accountName= b["AccountName"].ToString();
string SID = b["SID"].ToString();
Console.WriteLine("Trustees {0}\\{1} is {2}",
b["ReferencedDomainName"], b["AccountName"], b["SID"]);
}
}
}

if it doesn't work for you, I would suggest you try wbemtest.exe and issue
the same query and check the Win32_LogicalFileSecuritySetting object
associators to see what is returned.

Willy.
 
G

Guest

Looks like you are using it for local machine only. I would like to use this
for remote machine.
--
Thanks
SCS


Willy Denoyette said:
scsharma said:
Sorry, I didnot post the error message.
Error message simply says "Not Found"
and stack trace is

at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)\r\n at
System.Management.PropertyData.RefreshPropertyInfo()\r\n
at System.Management.PropertyData..ctor(ManagementBaseObject parent,
String
propName)\r\n at
System.Management.PropertyDataCollection.get_Item(String
propertyName)\r\n at
System.Management.ManagementBaseObject.GetPropertyValue(String
propertyName)\r\n at
System.Management.ManagementBaseObject.get_Item(String
propertyName)\r\n at Tester.testWMI() in
c:\\temp\\ecconsole\\batchconsoleapp\\wmicodesample.cs:line 40" string

This works for me (on XP and higher),

using System;
using System.Management;
using System.Collections;

class Tester {
public static void Main()
{
// Create FileObjectSecurity passing the file object (filepath) to the
ctor.
// Watch the double backslashes !!!!
string FileSystemObject = @"c:\\rssfeed.xml";

ManagementPath path = new ManagementPath( );

path.RelativePath = @"Win32_LogicalFileSecuritySetting.Path=" + "'" +
FileSystemObject + "'";
ManagementObject lfs = new ManagementObject(path);
foreach (ManagementBaseObject b in lfs.GetRelated())
{
string domainName= b["ReferencedDomainName"].ToString();
string accountName= b["AccountName"].ToString();
string SID = b["SID"].ToString();
Console.WriteLine("Trustees {0}\\{1} is {2}",
b["ReferencedDomainName"], b["AccountName"], b["SID"]);
}
}
}

if it doesn't work for you, I would suggest you try wbemtest.exe and issue
the same query and check the Win32_LogicalFileSecuritySetting object
associators to see what is returned.

Willy.
 
W

Willy Denoyette [MVP]

scsharma said:
Looks like you are using it for local machine only. I would like to use
this
for remote machine.
No matter, your problem is not related to local or remote machines, your
fileobject path is wrong, Didn't you notice ....

// Watch the double backslashes !!!!

in my sample.

Willy.
Willy Denoyette said:
scsharma said:
Sorry, I didnot post the error message.
Error message simply says "Not Found"
and stack trace is

at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)\r\n at
System.Management.PropertyData.RefreshPropertyInfo()\r\n
at System.Management.PropertyData..ctor(ManagementBaseObject parent,
String
propName)\r\n at
System.Management.PropertyDataCollection.get_Item(String
propertyName)\r\n at
System.Management.ManagementBaseObject.GetPropertyValue(String
propertyName)\r\n at
System.Management.ManagementBaseObject.get_Item(String
propertyName)\r\n at Tester.testWMI() in
c:\\temp\\ecconsole\\batchconsoleapp\\wmicodesample.cs:line 40" string

This works for me (on XP and higher),

using System;
using System.Management;
using System.Collections;

class Tester {
public static void Main()
{
// Create FileObjectSecurity passing the file object (filepath) to the
ctor.
// Watch the double backslashes !!!!
string FileSystemObject = @"c:\\rssfeed.xml";

ManagementPath path = new ManagementPath( );

path.RelativePath = @"Win32_LogicalFileSecuritySetting.Path=" +
"'" +
FileSystemObject + "'";
ManagementObject lfs = new ManagementObject(path);
foreach (ManagementBaseObject b in lfs.GetRelated())
{
string domainName= b["ReferencedDomainName"].ToString();
string accountName= b["AccountName"].ToString();
string SID = b["SID"].ToString();
Console.WriteLine("Trustees {0}\\{1} is {2}",
b["ReferencedDomainName"], b["AccountName"], b["SID"]);
}
}
}

if it doesn't work for you, I would suggest you try wbemtest.exe and
issue
the same query and check the Win32_LogicalFileSecuritySetting object
associators to see what is returned.

Willy.
 
G

Guest

I was able to execute same code(with "\\") on local machine but when i
specified the server name thats the time i started running into problem.
--
Thanks
SCS


Willy Denoyette said:
scsharma said:
Looks like you are using it for local machine only. I would like to use
this
for remote machine.
No matter, your problem is not related to local or remote machines, your
fileobject path is wrong, Didn't you notice ....

// Watch the double backslashes !!!!

in my sample.

Willy.
Willy Denoyette said:
Sorry, I didnot post the error message.
Error message simply says "Not Found"
and stack trace is

at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)\r\n at
System.Management.PropertyData.RefreshPropertyInfo()\r\n
at System.Management.PropertyData..ctor(ManagementBaseObject parent,
String
propName)\r\n at
System.Management.PropertyDataCollection.get_Item(String
propertyName)\r\n at
System.Management.ManagementBaseObject.GetPropertyValue(String
propertyName)\r\n at
System.Management.ManagementBaseObject.get_Item(String
propertyName)\r\n at Tester.testWMI() in
c:\\temp\\ecconsole\\batchconsoleapp\\wmicodesample.cs:line 40" string

--
Thanks
SCS

This works for me (on XP and higher),

using System;
using System.Management;
using System.Collections;

class Tester {
public static void Main()
{
// Create FileObjectSecurity passing the file object (filepath) to the
ctor.
// Watch the double backslashes !!!!
string FileSystemObject = @"c:\\rssfeed.xml";

ManagementPath path = new ManagementPath( );

path.RelativePath = @"Win32_LogicalFileSecuritySetting.Path=" +
"'" +
FileSystemObject + "'";
ManagementObject lfs = new ManagementObject(path);
foreach (ManagementBaseObject b in lfs.GetRelated())
{
string domainName= b["ReferencedDomainName"].ToString();
string accountName= b["AccountName"].ToString();
string SID = b["SID"].ToString();
Console.WriteLine("Trustees {0}\\{1} is {2}",
b["ReferencedDomainName"], b["AccountName"], b["SID"]);
}
}
}

if it doesn't work for you, I would suggest you try wbemtest.exe and
issue
the same query and check the Win32_LogicalFileSecuritySetting object
associators to see what is returned.

Willy.
 
K

Kevin Yu [MSFT]

Hi.

IMO, this has to work with double back slash. Will you try the sxample that
Willy has provided to see if it works on you machine?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

HI Kevin,
Thanks for replying back. I used the sample code and it works like champ as
long as i use it for local machine only. First of all how do i query remote
machine for information. I guess i have to use Path.Server property. Correct?
The moment i use that i get the error mentioned in my post.
I am using actual server name(and not the IPAddress). My managementpath
declaration looks like:

string fileObject = @"C:\Documents and Settings\myfolder\Temp.txt";
ManagementBaseObject ret = null;
ManagementPath path = new ManagementPath( );
path.Server = @"ServerName"; // Assuming that server name is server i am
trying to connect to

path.NamespacePath = @"root\cimv2";
path.RelativePath = @"Win32_LogicalFileSecuritySetting.Path=" + "'" +
fileObject + "'";
ManagementObject lfs = new ManagementObject(path);
Please let me know what i am doing wrong.
 
W

Willy Denoyette [MVP]

Like Kevin and I said before, you need the double back slashes in your
filepath!

string fileObject = @"C:\Documents and Settings\myfolder\Temp.txt";

should be...

string fileObject = @"C:\\Documents and Settings\\myfolder\\Temp.txt";

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top