繁体   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