2015-04-22 83 views
1

現在,我可以在本地運行時從我的ASP MVC Web應用程序執行Powershell Azure cmdlet和腳本,但是當我部署到Azure網站時,Powershell Azure cmdlet不起作用。是否可以從Web應用程序執行Powershell Azure CMDlet?

錯誤消息:

The term 'Get-AzureSubscription' 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.

我想這是行不通的,因爲「天藍色的cmdlet」沒有Web服務器

我能做些什麼上?有什麼建議?


嗨再次,即時通訊目前正在測試在Azure中的虛擬機一個IIS8託管的應用程序,但同樣,當應用程序部署到我不能使用Azure中的cmdlet的服務器上,在Azure的cmdlet已經安裝在虛擬機。

當我用Visual Studio在VM中調試應用程序時,我可以使用cmdlet,但是當部署應用程序時,cmdlet不會被識別。

還有一些我需要設置才能使用Azure cmdlet?


這是一個功能的一個例子,該函數填充下拉列表,其結果,在本地主機能正常工作,在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(); 
     ... 
    } 
+0

你想做什麼?我對Azure網站了解不多,但您需要安裝Azure PowerShell才能使用這些cmdlet:http://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/ –

回答

1

要回答具體的問題,不,在Azure中無法做到這一點Web Apps(以前稱爲Azure網站)。

雖然你有一些選擇。

  1. 的PowerShell命令,在大多數情況下,包裹Service Management APIResource Manager API。這些REST API取決於你想要做什麼,你可以直接使用它們,並使用HTTP/S調用直接從應用程序調用API。 我強烈建議採取這種方法

  2. 如果您確實必須從您的應用程序中依賴於Azure PowerShell,那麼您可以使用Azure Cloud Service Web角色,而使用Startup Task安裝Azure PowerShell cmdlet。

  3. 您可以採用IaaS方法,並在Azure虛擬機中託管您自己的IIS服務器(或服務器場),從而完全控制機器的配置。當然,如果你採取這種方法,你也承擔着維護虛擬機的全部責任。

就個人而言,我會盡我所能避免選項2和3.這只是不好的設計。在某種程度上,我甚至提到他們,因爲你可能會遇到其他問題,試圖讓他們工作。但是,我想在我的答案中徹底。

+0

感謝您的回答,我的應用程序需要很多特定的Powershell Azure cmdlet,因此我可以採用的最佳路徑是第三個。 – TassadarCRG

+0

嗨,我現在正在測試Azure虛擬機中的IIS8中託管的應用程序,但再次將應用程序部署到服務器時,我無法使用Azure Cmdlet,Azure cmdlet已安裝在虛擬機上。 當我用Visual Studio在VM中調試應用程序時,我可以使用cmdlet,但是當部署應用程序時,cmdlet不會被識別。 還有一些我需要設置才能使用Azure cmdlet? – TassadarCRG

+1

IIS的工作進程不會具有與您的開發環境相同的環境變量(即:%path%)。所以,你可能需要使用完整路徑來引用它們。儘管沒有看到你如何調用cmdlet,但很難說清楚。 –

相關問題