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

发布于 2024-08-04 02:26:05 字数 602 浏览 7 评论 0原文

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 ,我可以停止它。

直接在命令提示符下

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

The above is my code. I am not able to stop the IIS from the C# code, but if I execute

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

directly in command prompt, I can stop it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她说她爱他 2024-08-11 02:26:05

您不需要在参数中再次使用“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();

You don't need the "PsExec" again in the arguments, ie.:

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();
好倦 2024-08-11 02:26:05

有什么原因不能使用 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; 

}

Chris

Any reason you can't use the ServiceController class?

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

If you need to impersonate a user for the server in question, you can use this class:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文