简体   繁体   中英

Start exe from Windows Service using different credentials

I have a WCF service (exe) that I started via a console app using a different credential by passing the Domain, UserName and Password in the ProcessStartInfo() information. Using the code, the value for 'User name' in Task Manager for the 'exe' is 'TestUser'

    var dpmProcess = new Process
            {
                StartInfo = new ProcessStartInfo()
                {
                    WorkingDirectory = "C:\\Debug",
                    FileName = "",
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Normal,
                    Domain = "xx",
                    UserName = "TestUser",
                    Password = password, /*using SecureString*/
                    Verb = "runas"
                }
            };

            dpmProcess.Start();

I tried to do the same in a Windows Service Code, but my exe is not started. I used 'Impersonation' and my exe was started. However, the 'User name' in the Task Manager is 'SYSTEM' and not the 'UserName' (TestUser) I passed in Impersonation.

    //*start impersonator (2)
            using (var imp = new Impersonator(LOGIN, DOMAIN, PASSWORD))
            {
                WriteToFile("Starting..");

                var dpmProcess = new Process
                {
                    StartInfo = new ProcessStartInfo()
                    {
                        FileName = "xxx",
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        RedirectStandardInput = true,
                        UseShellExecute = false,
                        CreateNoWindow = true,
                        ErrorDialog = false,
                        WindowStyle = ProcessWindowStyle.Hidden
                    }
                };

                dpmProcess.Start();

                WriteToFile("Started..");
            }

在此处输入图像描述

Is it possible to start exe in Windows Service and see the Username as same as running the exe in Console App?

Thanks!

You can try using the the sc create command:

If i understand your needs correctly, based on this article:

enter link description here

You can use the sc command to run the service with the permissions of your desired user.

sc.exe [<servername>] create [<servicename>] obj= "[.\username]" password= "[password]"

For example:

sc.exe \\10.0.0.0\ create NewService binpath= c:\windows\system32\NewServ.exe obj= ".\Administrator" password= "MySuperSecurePassword@"

If that's not what you want to do you can also use schtasks.exe to reach you desired goal:

schtasks /create /sc ONCE /tn MyCoolTask /tr <path_to_executable> /ru "domain\user_name"

schtasks /run /tn MyCoolTask

Good luck, hope that helps:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM