使用PowerShell和FTP创建Azure网站 [英] Create an Azure Website with PowerShell and FTP

查看:212
本文介绍了使用PowerShell和FTP创建Azure网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个自动创建Azure网站并部署本地文件系统内容的PowerShell脚本。似乎Azure PowerShell SDK不提供将文件复制到网站的方法,因此我们希望将FTP用作部署方法。



要获得正确的FTP端点和证书我发现的唯一方法是调用Azure管理Rest API:获取网站的发布配置文件



但是,此API作为其他Azure Management API需要证书。我发现了以下教程
用Windows Azure构建真实世界的云应用程序,解释如何获取证书:

  $ s = Get-AzureSubscription  - 当前
$ thumbprint = $ s.Certificate.Thumbprint

不幸的是,对于当前的SDK $ s.Certificate 总是空,此属性不存在。如果我手动设置证书指纹,一切都按预期工作。



您对如何获得正确的订购证书有所了解吗?或者你有一个简单的替代方案来将本地文件部署到Azure网站? 解决方案

现在看来,您可以访问证书指纹使用

  $ thumbprint = $ s.DefaultAccount 

而不是

 #$ thumbprint = $ s.Certificate.Thumbprint 

看起来DefaultAccount的值与证书指纹完全相同。



仅供参考,我的完整脚本可以获得给定网站的发布配置文件:

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


#Get当前订阅
$ s = Get-AzureSubscription - 当前
如果(!$ s){throw无法获得Windows Azure订阅。}

#$ thumbprint = $ s .Certificate.Thumbprint #this代码不起作用
$ thumbprint = $ s.DefaultAccount
if(!$ thumbprint){throw无法获得订阅证书指纹。}

#获取证书从当地证书商店获取当前订阅
$ cert = Get-ChildItem Cert:\CurrentUser\My\ $ thumbprint
if(!$ cert){throw无法在Cert中找到订阅证书:drive。}

$ website = Get-AzureWebsite -Name $ WebsiteName $ b $ if if(!$ website){throw无法获取Windows Azure网站:$ WebsiteName。}

#撰写您将从中获得发布设置的REST API URI info
$ uri =https://management.core.windows.net:8443/{0}/services/WebSpaces/ {1} / sites / {2} / publishxml-f`
$ s.SubscriptionId,$ website.WebSpace,$ Website.Name

#从REST获取发布设置信息API
$ publishSettings = Invoke-RestMethod -Uri $ uri -Certificate $ cert -Headers @ {x-ms-version=2013-06-01}
如果(!$ publishSettings){throw无法获取Windows Azure网站publishSettings。}
$ b $ return $ publishSettings
}

注意:只有当您使用Import-AzurePublishSettingsFile连接到azure时,才能正常工作

任何人都可以确认安全

更新$ b

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

I need to write a PowerShell script that automatically creates an Azure Website and deploy the content of a local file system. Seems that Azure PowerShell SDK doesn't provide a way to copy files to a website so we want to use FTP as a deploy method.

To get the correct FTP endpoints and credentials the only way that I have found is to call an Azure Management Rest API: Get a Site’s Publish Profile.

But this API as other Azure Management API requires a certificate. I have found the following tutorial Building Real-World Cloud Apps with Windows Azure that explain how to get the certificate:

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

Unfortunately seems that with the current SDK $s.Certificate is always null, this property doesn't exists. If I manually set the certificate thumbprint everything works as expected.

Do you have an idea on how to get the correct subscription certificate? Or do you have an easy alternative to deploy local files to an Azure website?

解决方案

It seems that now you can access the certificate thumbprint using

$thumbprint = $s.DefaultAccount

instead of

#$thumbprint = $s.Certificate.Thumbprint

Seems that the DefaultAccount has exactly the same value as the certificate thumbprint.

Just for reference here is my complete script to obtain a publishing profile for a given website:

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
}

NOTE: this only works when you have connected to azure using Import-AzurePublishSettingsFile

Can anyone confirm that is safe to use DefaultAccount property?

UPDATE

If you use Kudu API to upload your site, like this, you don't need any certificate or publishing profile. You should read the user name and password using Get-AzureWebsite and the hostname is just yourwebsitename.scm.azurewebsites.net (note the scm segment). I suggest to use Kudu because is far more reliable and fast.

这篇关于使用PowerShell和FTP创建Azure网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆