簡體   English   中英

在部署到IIS服務器的ASP.NET應用程序中使用Powershell

[英]Using Powershell in an ASP.NET application deployed to an IIS server

我正在ASP.NET MVC中開發一個測試項目,其中Im使用System.Management.Automation執行一些PowerShell cmdlet。 一些cmdlet來自Azure Powershell模塊。

當我從Visual Studio運行它時,代碼運行良好。 但是,當我將網站發布到IIS時,某些cmdlet和這些腳本不起作用。

示例,請參見前面的注釋:

    var shell = PowerShell.Create();
    var script1 = "Get-AzureSubscription | Out-String"; // cant execute the cmdlet 
    var script2 = "C:\\inetpub\\wwwroot\\App1\\Scripts\\test.ps1"; //Cant execute the script.
    var script3 = "Get-Date"; //Work fine
    try
    {
     shell.Commands.AddScript(script); // here insert the script.
     Collection<PSObject> results = shell.Invoke();
     //Search for errors, if some error is found redirect to an Error page.
        if (shell.Streams.Error.Count > 0)
        {
         foreach (ErrorRecord err in shell.Streams.Error)
         {
          string error = err.ToString();
          TempData["pserror"] = error;
          return RedirectToAction("Powershellerror");                
         }
        }
         else 
          if (results.Count > 0)
          {
           foreach (var psObject in results)
           {
            string result2 = psObject.ToString();
            TempData["psoutput"] = result2;
            return RedirectToAction("PowershellOutput");
           }
          }

script1和script2都給出此錯誤:

術語'C:\\ inetpub \\ wwwroot \\ App1 \\ Scripts \\ test.ps1'不被識別為cmdlet,函數,腳本文件或可運行程序的名稱。 檢查名稱的拼寫,或者是否包含路徑,請驗證路徑是否正確,然后重試。

術語“ Get-AzureSubscription”不被視為cmdlet,函數,腳本文件或可運行程序的名稱。 檢查名稱的拼寫,或者是否包含路徑,請驗證路徑是否正確,然后重試。

可能是什么??? IIS安裝程序中缺少什么?

您可能需要導入Azure模塊,請按照此處的建議嘗試:

https://stackoverflow.com/a/6267517/1183475

var ps = PowerShell.Create(myRS);
ps.Commands.AddCommand("Import-Module").AddArgument(@"g:\...\PowerDbg.psm1")
ps.Invoke()

我沒有在此計算機上安裝Azure PS工具,但路徑應為:“ C:\\ Program Files(x86)\\ Microsoft SDKs \\ Windows Azure \\ PowerShell \\ Azure \\ Azure.psd1

暫無
暫無

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

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