1
votes

I am using the below script to deploy my .cspkg using powershell. I have created the appropriate certificate and uploaded to the Azure portal and the same has been installed in the build server.

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

If I execute this script manually on the build server, it works fine. I have customized tfs process template and using execute task to execute the powershell cmdlets. if this task is being executed , the following error has been thrown,

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

At 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 : NotSpecified: (:) [Get-AzureDeployment], HttpReq uestException + FullyQualifiedErrorId : System.Net.Http.HttpRequestException,Microsoft.W indowsAzure.Commands.ServiceManagement.HostedServices.GetAzureDeploymentCo mmand!

In this script if i use azure publish settings file , things are working fine with TFS. If I use certificate , the issue occurs, though if I use certificate and execute it manually (with out TFS process template ) working fine on the server. Any Clues ?

Thanks, Arun

1

1 Answers

1
votes

Finally I managed to solve this issue. The issue is that i was storing the certification in the Trusted Root ("cert:\localmachine\root") and referring this in the powershell rather just changed it to "cert:\localmachine\my" , things turned to working fine.