C# 无法使用带有PsExec的C代码启动/停止windows服务?

C# 无法使用带有PsExec的C代码启动/停止windows服务?,c#,iis-6,psexec,C#,Iis 6,Psexec,以上是我的代码。我无法从C#代码中停止IIS,但是如果我执行 PsExec\\\\Newton-u管理员-p密码IISReset/stop 直接在命令提示符下,我可以停止它。您不需要在参数中再次使用“PsExec”,即: Process process = new Process(); ProcessStartInfo psi = new ProcessStartInfo(@"C:/PsExec.exe"); psi.UseShellExecute = false; psi.RedirectS

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

PsExec\\\\Newton-u管理员-p密码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 = "PsExec \\\\Newton -u Administrator -p Password IISReset /stop";

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

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

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 svc = new ServiceController("servicename", "machinename");
svc.Stop();
使用System.ServiceProcess;
使用System.Security.Principal;
使用System.Runtime.InteropServices;
/// 
///模拟windows登录。
/// 
公共类ImpersonationUtil{
/// 
///模拟给定的登录信息。
/// 
///Windows登录名。
///密码
///域名
/// 
公共静态bool模拟(字符串登录、字符串密码、字符串域){
WindowsIdentity tempWindowsIdentity;
IntPtr令牌=IntPtr.Zero;
IntPtr-tokenDuplicate=IntPtr.Zero;
if(LogonUser)(登录、域、密码、LOGON32\u登录\u交互、,
LOGON32\u提供者\u默认值,ref令牌)!=0){
if(DuplicateToken(token,2,ref tokenDuplicate)!=0){
tempWindowsIdentity=新的WindowsIdentity(令牌复制);
impersonationContext=tempWindowsIdentity.Impersonate();
if(null!=impersonationContext)返回true;
}           
}
返回false;
}
/// 
///平易近人。
/// 
公共静态无效不做作(){
impersonationContext.Undo();
} 
[DllImport(“advapi32.dll”,CharSet=CharSet.Auto)]
公共静态外部内部登录用户(
字符串lpszUserName,
字符串lpszDomain,
字符串lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport(“advapi32.dll”,CharSet=System.Runtime.InteropServices.CharSet.Auto,SetLastError=true)]
公共外部静态int-DuplicateToken(
IntPtr hToken,
int模拟级别,
参考IntPtr hNewToken);
private const int LOGON32\u LOGON\u INTERACTIVE=2;
私有常量int LOGON32\u LOGON\u NETWORK\u CLEARTEXT=4;
private const int LOGON32_PROVIDER_DEFAULT=0;
私有静态WindowsImpersonationContext impersonationContext;
}


Chris

第二行中的斜杠也应该是反斜杠否,即使在删除PsExec之后,它也不起作用任何其他想法您是否有任何错误?PsExec真的安装到C:的根目录吗?我想远程停止iis,但有一些访问问题
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;