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

时间:2015-05-14 23:09:10

标签: c# asp.net powershell azure iis

我正在ASP.NET MVC中开发一个测试项目,我使用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”未被识别为a的名称   cmdlet,函数,脚本文件或可操作程序。检查拼写   如果包含名称,或者包含路径,请验证路径是否正确   纠正,然后再试一次。

可能是什么??? IIS设置中缺少什么东西?

1 个答案:

答案 0 :(得分:1)

您可能需要导入Azure模块,请按照此处的建议进行尝试:

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

public void makePetMakeNoise()
{
    int randnum = (int)(Math.random() *5);
    pet.makeNoise(randnum);
}

我没有在此计算机上安装Azure PS工具,但路径应为: “C:\ Program Files(x86)\ Microsoft SDKs \ Windows Azure \ PowerShell \ Azure \ Azure.psd1