使用PowerShell和FTP创建Azure网站

4
我需要编写一个PowerShell脚本,可以自动创建Azure网站并部署本地文件系统的内容。似乎Azure PowerShell SDK没有提供将文件复制到网站的方法,因此我们要使用FTP作为部署方法。
为了获取正确的FTP端点和凭据,我发现唯一的方法是调用Azure Management Rest API:获取网站的发布配置文件
但是,与其他Azure管理API一样,此API需要证书。我找到了以下教程:通过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。如果仍有问题,请告诉我们。 - Suwat Ch
@SuwatCh 谢谢,Kudu似乎是一个非常有趣的替代选择! - Davide Icardi
@SuwatCh 我最终使用了Kudu。这是我的代码片段:(https://gist.github.com/davideicardi/a8247230515177901e57) - Davide Icardi
1个回答

2

看起来,现在您可以使用以下方式访问证书指纹:

$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
}

注意:仅当您使用Import-AzurePublishSettingsFile连接到Azure时,此方法才有效。

有人能确认使用DefaultAccount属性是安全的吗?

更新

如果您使用Kudu API上传您的网站,像这样,则不需要任何证书或发布配置文件。您应该使用Get-AzureWebsite读取用户名和密码,主机名只需为yourwebsitename.scm.azurewebsites.net(注意scm段)。我建议使用Kudu,因为它更可靠且速度更快。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接