簡體   English   中英

從C#運行的PowerShell啟動/停止IIS應用程序池無效

[英]PowerShell run from C# to start/stop IIS application pools has no effect

我現在被困在這段小代碼上兩天了。 我有一個C#helper類來執行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());
                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM