gpt4 book ai didi

c# - iisreset 以编程方式覆盖服务器列表

转载 作者:行者123 更新时间:2023-11-30 21:53:36 25 4
gpt4 key购买 nike

我想通过 C# 代码以编程方式对具有执行此操作的帐户的服务器列表执行 iisreset。

对于本地机器来说很容易做到这一点,例如这是一个示例代码:

    // using ...
using System.Diagnostics;

public class YourForm : Form
{
// ...

private void yourButton_Click(object sender, EventArgs e)
{
Process.Start(@"C:\WINDOWS\system32\iisreset.exe", "/noforce");
}

// ...
}

还有:

    using System.ServiceProcess;

using (ServiceController controller = new ServiceController())
{
controller.MachineName = “My local or remote computer name”;
controller.ServiceName = “IIS Service Name”; // i.e “w3svc”

if (controller.Status != ServiceControllerStatus.Running)
{
// Start the service
controller.Start();

Log.Debug(“IIS has been started successfully, now checking again for webservice availability”);

}
else
{
// Stop the service
controller.Stop();

// Start the service
controller.Start();

Log.Debug(“IIS has been restarted successfully”);

}

}

但是如何为多个服务器执行此操作。

最佳答案

考虑到无需提供 iisreset 命令的完整路径,您的第一个代码片段应该可以完美运行。

实际上,从 CMD 或运行工具调用 IISRESET 时不需要完整路径。所以,这是同一个调用。

关于用户权限,有2种做法

  1. 您可以将所需的用户作为参数传递给 Process.Start

    Process.Start("iisreset", "server1", "admin", "admin password", "domain");

  2. 您可以像在代码中那样调用 Process.Start,然后确保以合适的用户运行您的应用程序

我在下面试过,效果很好

    static void Main(string[] args)
{
string[] servers = LoadServersFromFile();

foreach (string server in servers)
{
Process.Start("iisreset", server.Trim());
}
}

private static string[] LoadServersFromFile()
{
//just listed all servers comma separated in a text file, change this to any other approach fits for your case
TextReader reader = new StreamReader("Servers.txt");
return reader.ReadToEnd().Split(',');
}

关于c# - iisreset 以编程方式覆盖服务器列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33802067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com