【问题标题】:How Execute Azure Powershell cmdlets from a ASP app hosted in IIS如何从 IIS 中托管的 ASP 应用程序执行 Azure Powershell cmdlet
【发布时间】:2015-05-07 05:40:44
【问题描述】:

我正在开发一个自动化一些 Azure 服务的 ASP MVC 应用程序,比如创建一个 VM。为了做到这一点,我使用 Azure Powershell cmdlet。

该应用程序将托管在带有 IIS 和 SQL Server 的 Azure VM 中,我已经安装了 Azure cmdlet 模块来管理 de Azure 服务,我已经使用我的 Azure 订阅测试了 cmdlet,它们在 VM 中运行良好。

当我使用 localDB 和 IIS EXPRESS 在 localhost 上调试应用程序时,cmdlet 被成功识别并且运行良好,但是当我将应用程序部署到 IIS 时,CMDlet 无法识别。

“错误消息:术语“Get-AzureSubscription”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径正确,然后重试。”

我缺少什么?为了从 IIS 上部署的应用程序运行 cmdlet,我必须进行哪些设置?

更新: 这是一个函数的示例,该函数使用结果填充下拉列表,在 localhost 中它工作正常,在 IIS 中。

public IEnumerable<SelectListItem> getImageFamily()
    {
        var shell = PowerShell.Create();
        shell.Commands.AddScript("C:\\inetpub\\wwwroot\\appName\Scripts\\test.ps1");
        Collection<PSObject> results = shell.Invoke();

        List<SelectListItem> items = new List<SelectListItem>();
        foreach (var psObject in results)
        {
            string image = psObject.BaseObject.ToString();
            items.Add(new SelectListItem { Text = image, Value = image });
        }
        return items;
    }

那么这就是简单的脚本

(Get-AzureVMImage).ImageFamily | sort -Unique

我也试过

Import-Module -Name "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure"

(Get-AzureVMImage).ImageFamily | sort -Unique

也试过了(我删了一些代码...)

public IEnumerable<SelectListItem> getImageFamily()
    {
        ...
        shell.Commands.AddScript("(Get-AzureVMImage).ImageFamily | sort -Unique");
        Collection<PSObject> results = shell.Invoke();
        ...
    }

当我在脚本文件中运行指令时,我得到:

术语“C:\inetpub\wwwroot\appName\Scripts\test.ps1”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,如果包含路径,请验证路径是否正确,然后重试。

:(

【问题讨论】:

  • 您是否在 IIS 上安装了 Azure PowerShell 模块?
  • Azure Powershell 模块安装在 IIS 所在的 VM 上,不知道是否需要在 IIS 中设置其他内容。

标签: c# asp.net-mvc powershell azure powershell-cmdlet


【解决方案1】:

我在尝试自动部署到 azure 时也遇到了这个问题

我的解决方案是在脚本中加载模块

Write-Verbose "Improt Azure Powershell cmdlets"
Import-Module -Name "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure"
Import-Module -Name "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager"

附: 确保您的虚拟机中的路径相同

【讨论】:

  • 该死我的 Azure 订阅今天刚刚到期,我会尽快尝试重新使用 Azure >。
  • 好吧,我什至无法运行脚本The term 'C:\pathtoscript\import.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  • 我刚刚运行了该导入语句,现在 Get-AzureVM 不再有效。我该如何重置?
  • 我刚刚从删除程序中修复了 Powershell Azure 安装。仍然不确定如何在脚本中修复它。
猜你喜欢
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-06
相关资源
最近更新 更多