[英]How Execute Azure Powershell cmdlets from a ASP app hosted in IIS
我正在使用ASP MVC应用程序,该应用程序可以自动执行一些Azure服务,例如创建VM。 为了做到这一点,我使用Azure Powershell cmdlet。
该应用程序将托管在带有IIS和SQL Server的Azure VM中,我已经安装了Azure cmdlet模块来管理de Azure服务,已经使用Azure订阅测试了cmdlet,它们在VM中运行良好。
当我使用localDB和IIS EXPRESS在本地主机上调试应用程序时,cmdlet已成功识别并正常运行,但是当我将应用程序部署到IIS时,CMDlet无法识别。
“错误消息:术语'Get-AzureSubscription'不被识别为cmdlet,函数,脚本文件或可运行程序的名称。请检查名称的拼写,或者是否包含路径,请验证路径是否正确然后再试一次。”
Uopdate:这是一个函数的示例,该函数在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,函数,脚本文件或可运行程序的名称。 检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试。
:(
在尝试自动部署到天蓝色时也遇到了这个问题
我的解决方案是在脚本中加载模块
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"
PS确保虚拟机中的路径相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.