从c#运行PowerShell来启动/停止IIS应用程序池没有效果
本文关键字:应用 IIS 应用程序 程序池 有效果 停止 运行 PowerShell 启动 | 更新日期: 2023-09-27 18:02:58
这段代码我已经写了两天了。我有一个c#辅助类来执行PowerShell脚本。我基本上使用PowerShell.Create()
初始化PowerShell实例,然后使用AddScript
编写命令,然后同步调用Invoke
方法。
现在我试图停止一个windows服务和一个IIS应用程序池。Windows服务停止,但对IIS应用程序池没有影响。
using (var powerShell = PowerShell.Create())
{
// PowerShell doesn't stop application pool
powerShell.AddScript("Stop-WebAppPool -Name application_name");
// But it stops windows service
powerShell.AddScript("Stop-Service service_name");
var result = powerShell.Invoke();
}
当我通过ISE执行它们时,两个脚本都可以工作。有什么问题吗?我想我错过了关于PowerShell的一些东西
你可以这样写
using (PowerShell shell = PowerShell.Create())
{
shell.AddScript(@"Import-Module WebAdministration;");
shell.AddScript(@"Stop-Website -Name 'Default Web Site';");
shell.AddScript(@"Stop-WebAppPool -Name 'DefaultAppPool';");
shell.Invoke();
if (shell.HadErrors)
{
foreach (ErrorRecord _ErrorRecord in shell.Streams.Error)
{
Console.WriteLine(_ErrorRecord.ToString());
}
}
}