Quantcast
Channel: Task Scheduler Managed Wrapper
Viewing all articles
Browse latest Browse all 2206

New Post: Is there a way to find the logged in user's "domain\name" when creating a task in code running as SYSTEM?

$
0
0
I have scoured the internet and there are suggestions that work for certain scenarios, but none work for me. I'm creating a task in a custom action inside the installer. The installer runs as SYSTEM, so instead of my domain\username I get SYSTEM. And, I'm installing a service, which also runs as SYSTEM. Is there a way of getting the actual current user's name, short of asking the user to enter manually? Here's my code:

//Works, I get "Bob-PC\Bob" if I run the code as a user. If it's run as a system
// I get "WORKGROUP\SYSTEM"
string user = Environment.UserDomainName + "\\"+ Environment.UserName;
// doesn't work, returns "NT AUTHORITY\SYSTEM"
string user2 = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
// doesn't work at all, this is not ASP.NET 
string user3 = "";//Request.LogonUserIdentity.Name;
// doesn't work, my thread is unnamed
string user4 = System.Threading.Thread.CurrentPrincipal.Identity.Name;

MessageBox.Show("user " + user + "\nuser2 " + user2 + "\nuser3 " + user3 + "\nuser4 " + user4);
tf.RegisterTaskDefinition("MyTask", td,
    TaskCreation.CreateOrUpdate,
    user,
    null,
    TaskLogonType.InteractiveToken,
    null);

Viewing all articles
Browse latest Browse all 2206

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>