gpt4 book ai didi

powershell - 使用 powershell 进行 Azure 部署,错误 : Get-AzureDeployment : An error occurred while sending the request

转载 作者:行者123 更新时间:2023-12-03 03:15:18 32 4
gpt4 key购买 nike

我正在使用以下脚本通过 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 字符:3 + Get-AzureDeployment -ServiceName“ppmcd”-Slot“staging”#-ErrorVariable 一个-E... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~ + 类别信息:未指定:(:) [获取 AzureDeployment]、HttpReq 异常 + FullQualifiedErrorId:System.Net.Http.HttpRequestException,Microsoft.W indowsAzure.Commands.ServiceManagement.HostedServices.GetAzureDeploymentCo 要求!

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

谢谢,阿伦

最佳答案

终于我成功解决了这个问题。问题是我将证书存储在受信任的根(“cert:\localmachine\root”)中,并在 powershell 中引用它,而只是将其更改为“cert:\localmachine\my”,事情变得正常。

关于powershell - 使用 powershell 进行 Azure 部署,错误 : Get-AzureDeployment : An error occurred while sending the request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28516098/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com