[英]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.