Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

Problems running WMI code through C# as scheduled task

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ryan McFall - 22 Sep 2007 13:26 GMT
I am running the following code as part of a scheduled task; the
objective of the code is to determine whether or not someone is logged
on to the workstation in an interactive session, and if so, if that
person is the same one as the one running the task.

ManagementScope ms = new ManagementScope(ManagementPath.DefaultPath);
System.Management.ManagementObject o;
SelectQuery q = new SelectQuery("Win32_LoggedOnUser"); // only
interactive logons
ManagementObjectSearcher query = new ManagementObjectSearcher(ms, q);

ManagementObjectCollection queryCollection = query.Get();
LinkedList<UserInfo> users = new LinkedList<UserInfo>();

foreach (ManagementObject loggedOnUser in queryCollection)
{
 //  Dependent is a Win32_LoginSession object
 ManagementObject session = new
ManagementObject(loggedOnUser["Dependent"].ToString());
 if ((session["LogonType"].ToString()) == "2")  {  // is
interactive?
   //  Antecedent is a Win32_Account object
   Console.WriteLine("About to access the antecedent");
   ManagementObject account = new

ManagementObject(loggedOnUser["Antecedent"].ToString());

   String loginID = session["LogonID"].ToString();

   String SID = account["SID"].ToString();

When this code is run by the logged on user within Visual Studio, it
works fine.  But, when it's run as a scheduled task, running as
Administrator while another user is logged in, an NotFound exception
is thrown when trying to access the "SID" property of the account
object; actually, attempts to access any properties of this object
fail.  Accesses to the properties of the session object are
successful.

Does anyone know why this would be the case?

Thanks in advance,
Ryan
Kevin_E - 25 Sep 2007 04:10 GMT
It has been a long time since I dealt with Windows security, but I have a
fuzy memory that sometimes one has to enable one's permissions.  See the
AdjustTokenPriveledges function:  
http://msdn2.microsoft.com/en-us/library/aa375202.aspx.  

This looks good at a glance:
http://msdn.microsoft.com/msdnmag/issues/05/03/TokenPrivileges/default.aspx

I find the best tool for debugging things like this can be the NT Event log.
Make sure your system is configured to log all security access errors and
you will see exactly what priveledge is tripping you up (if that is your
problem).

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.