2014-11-03 120 views
3

我需要編寫一個自動創建Azure網站並部署本地文件系統內容的PowerShell腳本。似乎Azure PowerShell SDK不提供將文件複製到網站的方法,因此我們希望將FTP用作部署方法。使用PowerShell和FTP創建Azure網站

要獲得正確的FTP端點和憑證,我發現的唯一方法是調用Azure管理Rest Rest API:Get a Site’s Publish Profile

但是,作爲其他Azure Management API的此API需要證書。我發現下面的教程 Building Real-World Cloud Apps with Windows Azure解釋如何拿到證書:

$s = Get-AzureSubscription -Current 
$thumbprint = $s.Certificate.Thumbprint 

遺憾的是似乎與目前的SDK $s.Certificate總是空,這個屬性不存在。如果我手動設置證書指紋,一切都按預期工作。

您有關於如何獲得正確的訂購證書的想法嗎?或者您是否有將本地文件部署到Azure網站的簡單方法?

+1

另一種方法是通過SCM(Kudu)站點部署文件。查看可用的REST API(https://github.com/projectkudu/kudu/wiki/REST-API);特別是VFS或Zip。讓我們知道如果仍然問題。 – 2014-11-04 04:08:44

+0

@SuwatCh謝謝,Kudu似乎是一個非常有趣的選擇! – 2014-11-05 08:39:26

+0

@SuwatCh我結束了使用kudu。這裏[我的要點](https://gist.github.com/davideicardi/a8247230515177901e57) – 2014-11-07 10:52:24

回答

1

看來,現在你可以使用

$thumbprint = $s.DefaultAccount 

代替

#$thumbprint = $s.Certificate.Thumbprint 

訪問證書指紋似乎DefaultAccount具有完全相同的值作爲證書指紋。

僅作參考,這裏是我完整的腳本,以獲得給定網站上發佈個人資料:

Function get-AzureWebSitePublishXml 
{ 
    Param(
     [Parameter(Mandatory = $true)] 
     [String]$WebsiteName 
    ) 

    # Get the current subscription 
    $s = Get-AzureSubscription -Current 
    if (!$s) {throw "Cannot get Windows Azure subscription."} 

    #$thumbprint = $s.Certificate.Thumbprint #this code doesn't work anymore 
    $thumbprint = $s.DefaultAccount 
    if (!$thumbprint) { throw "Cannot get subscription cert thumbprint."} 

    # Get the certificate of the current subscription from your local cert store 
    $cert = Get-ChildItem Cert:\CurrentUser\My\$thumbprint 
    if (!$cert) {throw "Cannot find subscription cert in Cert: drive."} 

    $website = Get-AzureWebsite -Name $WebsiteName 
    if (!$website) {throw "Cannot get Windows Azure website: $WebsiteName."} 

    # Compose the REST API URI from which you will get the publish settings info 
    $uri = "https://management.core.windows.net:8443/{0}/services/WebSpaces/{1}/sites/{2}/publishxml" -f ` 
     $s.SubscriptionId, $website.WebSpace, $Website.Name 

    # Get the publish settings info from the REST API 
    $publishSettings = Invoke-RestMethod -Uri $uri -Certificate $cert -Headers @{"x-ms-version" = "2013-06-01"} 
    if (!$publishSettings) {throw "Cannot get Windows Azure website publishSettings."} 

    return $publishSettings 
} 

注意:這隻能當你將使用進口AzurePublishSettingsFile天青

任何人都可以確認可以安全使用DefaultAccount屬性?

UPDATE

如果使用Kudu API上傳您的網站,like this,你不需要任何證書或發佈配置文件。您應該使用Get-AzureWebsite來讀取用戶名和密碼,主機名只是yourwebsitename.scm.azurewebsites.net(注意scm段)。我建議使用Kudu,因爲它更加可靠和快速。