繁体   English   中英

无法在PsExec中使用C#代码启动/停止Windows服务?

[英]Cannot Start /Stop windows service using C# code with PsExec?

Process process = new Process();

ProcessStartInfo psi = new ProcessStartInfo(@"C:/PsExec.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.WindowStyle = ProcessWindowStyle.Minimized;
psi.CreateNoWindow = true;
psi.Arguments = "PsExec \\\\Newton -u Administrator -p Password IISReset /stop";

process.StartInfo = psi;
process.Start();

以上是我的代码。 我无法从C#代码停止IIS,但是如果执行

PsExec \\\\\\\\Newton -u Administrator -p Password IISReset /stop

直接在命令提示符下,我可以停止它。

您不需要在参数中再次使用“ PsExec”,即:

Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo(@"C:\PsExec.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.WindowStyle = ProcessWindowStyle.Minimized;
psi.CreateNoWindow = true;
psi.Arguments = "\\\\Newton -u Administrator -p Password IISReset /stop";

process.StartInfo = psi;
process.Start();

有什么原因不能使用ServiceController类?

ServiceController svc = new ServiceController("servicename", "machinename");
svc.Stop();

如果需要模拟所涉及服务器的用户,则可以使用此类:

using System.ServiceProcess;
using System.Security.Principal;
using System.Runtime.InteropServices;

/// <summary>
/// Impersonate a windows logon.
/// </summary>

public class ImpersonationUtil {

    /// <summary>
/// Impersonate given logon information.
/// </summary>
/// <param name="logon">Windows logon name.</param>
/// <param name="password">password</param>
/// <param name="domain">domain name</param>
/// <returns></returns>
public static bool Impersonate( string logon, string password, string domain ) {
    WindowsIdentity tempWindowsIdentity;
    IntPtr token = IntPtr.Zero;
    IntPtr tokenDuplicate = IntPtr.Zero;

    if( LogonUser( logon, domain, password, LOGON32_LOGON_INTERACTIVE, 
            LOGON32_PROVIDER_DEFAULT, ref token) != 0 ) {

        if ( DuplicateToken( token, 2, ref tokenDuplicate ) != 0 ) {
            tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
            impersonationContext = tempWindowsIdentity.Impersonate();
            if ( null != impersonationContext ) return true;                
        }           
    }

    return false;
}

/// <summary>
/// Unimpersonate.
/// </summary>
public static void UnImpersonate() {
    impersonationContext.Undo();
} 

[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern int LogonUser( 
    string lpszUserName, 
    String lpszDomain,
    String lpszPassword,
    int dwLogonType, 
    int dwLogonProvider,
    ref IntPtr phToken );

[DllImport("advapi32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)]
public extern static int DuplicateToken(
    IntPtr hToken, 
    int impersonationLevel,  
    ref IntPtr hNewToken );

private const int LOGON32_LOGON_INTERACTIVE = 2;
private const int LOGON32_LOGON_NETWORK_CLEARTEXT = 4;
private const int LOGON32_PROVIDER_DEFAULT = 0;
private static WindowsImpersonationContext impersonationContext; 

}

克里斯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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