我正在开发一个自动化某些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,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。"
Uopdate: 这是一个函数的示例,该函数在IIS中填充带有结果的下拉列表,在localhost中就可以了。
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();
...
}
当我在脚本文件中运行指令时,我得到了:
术语&#39; C:\ inetpub \ wwwroot \ appName \ Scripts \ test.ps1&#39;不被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。
:(
答案 0 :(得分:1)
我的案例中的解决方案是在脚本
中加载模块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"
P.S。 确保VM的路径相同