2009-09-02 6 views
1
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(); 

Ce qui précède est mon code. Je ne suis pas en mesure d'arrêter l'IIS à partir du code C#, mais si j'exécute directementImpossible de démarrer/arrêter le service Windows en utilisant le code C# avec PsExec?

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

dans l'invite de commande, je peux l'arrêter.

Répondre

0

Vous n'avez pas besoin de nouveau « PsExec » dans les arguments, à savoir .:

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(); 
+0

également la barre oblique dans la deuxième ligne devrait être une barre oblique inverse – Oliver

+0

pas même après avoir supprimé le PsExec son ne fonctionne pas toute autre idée – Selwyn

+0

Avez-vous des erreurs? Est-ce que PsExec * est vraiment installé à la racine de C:? –

0

une raison quelconque vous ne pouvez pas utiliser la classe ServiceController?

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

Si vous avez besoin d'usurper l'identité d'un utilisateur pour le serveur en question, vous pouvez utiliser cette classe:

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; 

}

Chris

+0

je veux arrêter l'iis à distance, mais avoir un problème d'accès – Selwyn

Questions connexes