使用PowerShell进行Azure部署,错误:Get-AzureDeployment:发送请求时发生错误

时间:2015-02-14 13:27:50

标签: powershell azure deployment azure-deployment

我使用以下脚本使用powershell部署我的.cspkg。我已创建相应的证书并上传到Azure门户,并且已在构建服务器中安装了相同的证书。

param 
( 
    [string]$subscriptionID = "",
    [string]$subscriptionName = "XXXXXXX",
    [string]$thumbprint = "",
    [string]$serviceName = "",
    [string]$slot = "Production",
    [string]$storageAccountName ="",
    [string]$packageLocation = "",
    [string]$serviceConfiguration = "",
    [string]$certificateStore = "cert:\localmachine\root",
    [string]$timeStampFormat = "g",
    [string]$upgradeMode = "Auto",
    [string]$action="deploy",
    [int]$alwaysDeleteExistingDeployments = 1,
    [int]$enableDeploymentUpgrade = 1,
    [string]$tag = ""
)

$certThumbprint = $thumbprint.ToUpper()
$certPath = $certificateStore + "\\" + $certThumbprint
$cert = get-item $certPath

$buildLabel = ""
$packageName = ""
$a = Get-Date
if ($tag -eq "") 
{
    $buildLabel = "Daily Build" + "-" + $a.ToShortDateString() + "-" + $a.ToShortTimeString()
    $packageName = $serviceName + $a.ToString("yyyyMMdd") + ".cspkg"
} 
else 
{
    $buildLabel = "BuildTag-" + $tag + "-" + $a.ToShortDateString() + "-" + $a.ToShortTimeString()
    $packageName = $serviceName + "-" + $tag + "-" + $a.ToString("yyyyMMdd") + ".cspkg"

}

Get-ChildItem "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\*.psd1" | ForEach-Object {Import-Module $_}

function Publish()
{
    $deployment = Get-AzureDeployment -ServiceName $serviceName -Slot $slot -ErrorVariable a -ErrorAction silentlycontinue 
    if ($a[0] -ne $null)
    {
        Write-Output "$(Get-Date -f $timeStampFormat) - No deployment is detected. Creating a new deployment. "
    }
    #check for existing deployment and then either upgrade, delete + deploy, or cancel according to $alwaysDeleteExistingDeployments and $enableDeploymentUpgrade boolean variables
    if ($deployment.Name -ne $null)
    {
        switch ($alwaysDeleteExistingDeployments)
        {
            1 
            {
                switch ($enableDeploymentUpgrade)
                {
                    1  #Update deployment inplace (usually faster, cheaper, won't destroy VIP)
                    {
                        Write-Output "$(Get-Date -f $timeStampFormat) - Deployment exists in $servicename.  Upgrading deployment."
                        UpgradeDeployment
                    }
                    0  #Delete then create new deployment
                    {
                        Write-Output "$(Get-Date -f $timeStampFormat) - Deployment exists in $servicename.  Deleting deployment."
                        DeleteDeployment
                        CreateNewDeployment

                    }
                } # switch ($enableDeploymentUpgrade)
            }
            0
            {
                Write-Output "$(Get-Date -f $timeStampFormat) - ERROR: Deployment exists in $servicename.  Script execution cancelled."
                #exit
            }
        } #switch ($alwaysDeleteExistingDeployments)
    } else {
            CreateNewDeployment
    }
}

function CreateNewDeployment()
{
    write-progress -id 3 -activity "Creating New Deployment" -Status "In progress"
    Write-Output "$(Get-Date -f $timeStampFormat) - Creating New Deployment: In progress"

    $opstat = New-AzureDeployment -Slot $slot -Package $packageLocation -Configuration $serviceConfiguration -label $buildLabel -ServiceName $serviceName -ErrorAction stop

    $completeDeployment = Get-AzureDeployment -ServiceName $serviceName -Slot $slot
    $completeDeploymentID = $completeDeployment.deploymentid

    write-progress -id 3 -activity "Creating New Deployment" -completed -Status "Complete"
    Write-Output "$(Get-Date -f $timeStampFormat) - Creating New Deployment: Complete, Deployment ID: $completeDeploymentID"
}

function UpgradeDeployment()
{
    write-progress -id 3 -activity "Upgrading Deployment" -Status "In progress"
    Write-Output "$(Get-Date -f $timeStampFormat) - Upgrading Deployment: In progress"

    $configFile = Get-Item $serviceConfiguration

    # perform Update-Deployment
    $setdeployment = Set-AzureDeployment -Upgrade `
    -Mode $upgradeMode `
    -Slot $slot `
    -Package (Get-Item $packageLocation).FullName `
    -Configuration $configFile.FullName `
    -label $buildLabel `
    -ServiceName $serviceName -Force -ErrorAction stop

    $completeDeployment = Get-AzureDeployment -ServiceName $serviceName -Slot $slot
    $completeDeploymentID = $completeDeployment.deploymentid

    write-progress -id 3 -activity "Upgrading Deployment" -completed -Status "Complete"
    Write-Output "$(Get-Date -f $timeStampFormat) - Upgrading Deployment: Complete, Deployment ID: $completeDeploymentID"
}

function DeleteDeployment()
{

    write-progress -id 2 -activity "Deleting Deployment" -Status "In progress"
    Write-Output "$(Get-Date -f $timeStampFormat) - Deleting Deployment: In progress"

    #WARNING - always deletes with force
    $removeDeployment = Remove-AzureDeployment -Slot $slot -ServiceName $serviceName -Force -ErrorAction stop

    write-progress -id 2 -activity "Deleting Deployment: Complete" -completed -Status $removeDeployment
    Write-Output "$(Get-Date -f $timeStampFormat) - Deleting Deployment: Complete"
}

Remove-AzureSubscription -SubscriptionName $SubscriptionName -Force

#configure powershell with publishsettings for your subscription
Set-AzureSubscription -SubscriptionName $SubscriptionName -SubscriptionId $subscriptionID -Certificate $cert -CurrentStorageAccountName $storageAccountName

#set remaining environment variables for Azure cmdlets
$subscription = Select-AzureSubscription $SubscriptionName

#main driver - publish & write progress to activity log
Write-Output "$(Get-Date -f $timeStampFormat) - Azure Cloud Service deploy script started."
if ($action -eq "deploy") {
    Write-Output "$(Get-Date -f $timeStampFormat) - Preparing deployment of $buildLabel for $subscriptionName with Subscription ID $subscriptionID."
    Publish
    $deployment = Get-AzureDeployment -slot $slot -serviceName $servicename
    $deploymentUrl = $deployment.Url
    Write-Output "$(Get-Date -f $timeStampFormat) - Created Cloud Service with URL $deploymentUrl."
    Write-Output "$(Get-Date -f $timeStampFormat) - Azure Cloud Service deploy script finished."
} elseif ($action -eq "delete") {
    Write-Output "$(Get-Date -f $timeStampFormat) - Preparing to delete cloudservice in $serviceName,$slot for sub: $subscriptionName,$subscriptionID"
    $deployment = Get-AzureDeployment -ServiceName $serviceName -Slot $slot -ErrorVariable a -ErrorAction silentlycontinue 
    if ($a[0] -ne $null)
    {
        Write-Output "$(Get-Date -f $timeStampFormat) - No deployment found"
    } else {
        DeleteDeployment
        Write-Output "$(Get-Date -f $timeStampFormat) - Deleted successfully"
    }
}

如果我在构建服务器上手动执行此脚本,它可以正常工作。我已经定制了tfs流程模板并使用执行任务来执行powershell cmdlet。如果正在执行此任务,则抛出以下错误,

Get-AzureDeployment : An error occurred while sending the request.

在E:\ B \ 61 \ 378 \ src \ PPM Azure \ Production \ Web \ PSScript \ PublishClo  udServiceThumPrint.ps1:155 char:3  + Get-AzureDeployment -ServiceName“ppmcd”-Slot“staging”#-ErrorVariable  a -E ...  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~  + CategoryInfo:未指定:(:) [Get-AzureDeployment],HttpReq  uestException  + FullyQualifiedErrorId:System.Net.Http.HttpRequestException,Microsoft.W  indowsAzure.Commands.ServiceManagement.HostedServices.GetAzureDeploymentCo  mmand!

在这个脚本中,如果我使用azure发布设置文件,那么TFS工作正常。如果我使用证书,则会出现问题,但是如果我使用证书并手动执行它(使用TFS流程模板)在服务器上正常工作。任何线索?

谢谢, 阿伦

1 个答案:

答案 0 :(得分:1)

最后我设法解决了这个问题。问题是我将证书存储在受信任的根目录(“cert:\ localmachine \ root”)中并在powershell中引用它,而只是将其更改为“cert:\ localmachine \ my”,事情变得正常。< / p>

相关问题