简体   繁体   English

如何使用Powershell更新VSTS发布作业代理程序池

[英]How to update VSTS release job agent pool using powershell

I am trying to update VSTS release definition agent pool using powershell. 我正在尝试使用Powershell更新VSTS版本定义代理程序池。 Able to get the agent id and release definition details. 能够获取代理ID和发布定义的详细信息。

Powershell : 电源外壳 :

$User = 'xxxx@software.com'
$PersonalAccessToken = 'xxxx'
$base64authinfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f  $User, $PersonalAccessToken)))
$vstsAccount = "software"
$resource = 'Enterprise'

#Get the Agent ID
$uri = "https://$vstsAccount.visualstudio.com/_apis/distributedtask/pools/"
$projects = Invoke-RestMethod -Method Get -ContentType application/json -Uri $uri -Headers @{Authorization=("Basic {0}" -f $base64authinfo)} | ConvertTo-Json
$projects = $projects | ConvertFrom-Json
Write-Host "Json Body :" $projects
$id = $projects.value | ? name -eq 'Hosted VS2017'
Write-Host "Agent Pool Id :" $id.id


#Get release definition details
$uri2 = "https://vsrm.dev.azure.com/$vstsAccount/$resource/_apis/release/definitions/860?api-version=5.0"
$projects2 = Invoke-RestMethod -Method Get -ContentType application/json -Uri $uri2 -Headers @{Authorization=("Basic {0}" -f $base64authinfo)} | ConvertTo-Json
$projects2 = $projects2 | ConvertFrom-Json
Write-Host "Json Body :" $projects2

Response : 回应:

@{
source=userInterface; 
revision=84; 
description=; 
createdBy=; 
createdOn=2019-02-15T07:27:04.357Z; 
modifiedBy=; 
modifiedOn=2019-04-04T17:27:30.487Z; 
isDeleted=False; 
variables=; 
variableGroups=System.Object[]; 
environments=System.Object[]; 
artifacts=System.Object[]; 
triggers=System.Object[]; 
releaseNameFormat=Release-$(rev:r); 
tags=System.Object[]; 
pipelineProcess=; 
properties=; 
id=860; 
name=JMeterReleaseJob; 
path=\; 
projectReference=; 
url=https://vsrm.dev.azure.com/software/18979a9c-xxxx-xxxx-xxxx-f3fa4a82df3c/_apis/Release/definitions/860; _links=
}

Agent ID json : 代理ID json:

"value":  [
                  {
                      "createdOn":  "2016-03-23T12:04:35.723Z",
                      "autoProvision":  true,
                      "autoSize":  true,
                      "targetSize":  null,
                      "agentCloudId":  null,
                      "createdBy":  null,
                      "owner":  null,
                      "id":  2,
                      "scope":  "3e93f9e6-xxxx-xxxx-xxxx-994d196e1121",
                      "name":  "Hosted",
                      "isHosted":  true,
                      "poolType":  "automation",
                      "size":  10
                  },
                  {
                      "createdOn":  "2017-03-21T00:40:47.527Z",
                      "autoProvision":  true,
                      "autoSize":  true,
                      "targetSize":  null,
                      "agentCloudId":  null,
                      "createdBy":  null,
                      "owner":  null,
                      "id":  4,
                      "scope":  "3e93f9e6-xxxx-xxxx-xxxx-994d196e1121",
                      "name":  "Hosted VS2017",
                      "isHosted":  true,
                      "poolType":  "automation",
                      "size":  10
                  },
                  {
                      "createdOn":  "2018-05-04T10:20:59.69Z",
                      "autoProvision":  false,
                      "autoSize":  true,
                      "targetSize":  null,
                      "agentCloudId":  null,
                      "createdBy":  "@{displayName=Kabir Sharan; url=https://spsprodweu3.vssps.visualstudio.com/A3e42757e-xxxx-xxxx-xxxx-ee414eb0f5b1/_apis/Identities/8fe521c4-xxxx-xxxx-xxxx-6ca5f1b30c10; _links=; 
id=8fe521c4-xxxx-xxxx-xxxx-6ca5f1b30c10; uniqueName=Kabir@software.com; imageUrl=https://condecosoftware.visualstudio.com/_apis/GraphProfile/MemberAvatars/aad.OGZlNTIjhhLWFlZTgtNmNhNWYxY
jMwYzEw; descriptor=aad.OGZlNTIxYzQtYmQwNygtNmNhNWYxYjMwYzEw}",
                      "owner":  "@{displayName=Kabir Sharan; url=https://spsprodweu3.vssps.visualstudio.com/A3e42757e-xxxx-xxxx-xxxx-ee414eb0f5b1/_apis/Identities/8fe521c4-xxxx-xxxx-xxxx-6ca5f1b30c10; _links=; id=8
fe521c4-xxxx-xxxx-xxxx-6ca5f1b30c10; uniqueName=Kabir@software.com; imageUrl=https://condecosoftware.visualstudio.com/_apis/GraphProfile/MemberAvatars/aad.OGZlNLWFlZTgtNmNhNWYxYjMwY
zEw; descriptor=aad.OGZlNTIxYzQtYmQwNy03MNmNhNWYxYjMwYzEw}",
                      "id":  10,
                      "scope":  "3e93f9e6-xxxx-xxxx-xxxx-994d196e1121",
                      "name":  "PaasAgent1",
                      "isHosted":  false,
                      "poolType":  "automation",
                      "size":  0
                  }]

Now i want to update agent pool in release definition. 现在,我想更新发行版定义中的代理池。

Reference : In VSTS, can I bulk change the agent queue? 参考: 在VSTS中,可以批量更改代理队列吗?

You'll need to modify the object returned when you invoke $uri2 then use the PUT HTTP method to update the release definition in Azure DevOps. 您需要修改在调用$ uri2时返回的对象,然后使用PUT HTTP方法更新Azure DevOps中的发行版定义。

Converting $projects2 to and from JSON is unnecessary, the objects can be manipulated directly. 无需在$ projects2与JSON之间进行转换,可以直接操作这些对象。 Remove | ConvertTo-Json 删除| ConvertTo-Json | ConvertTo-Json from the end of the Invoke-RestMethod that sets $project2 and also the line $projects2 = $projects2 | ConvertFrom-Json 从Invoke-RestMethod的结尾处的| ConvertTo-Json设置$ project2以及行$projects2 = $projects2 | ConvertFrom-Json $projects2 = $projects2 | ConvertFrom-Json . $projects2 = $projects2 | ConvertFrom-Json

To change the change the Agent Pool you'll need to update the queueId property of the relevant deploy phase in the relevant environment. 要更改代理程序池的更改,您需要在相关环境中更新相关部署阶段的queueId属性。 If you had 1 environment with 1 deploy phase this would by done by 如果您有1个环境和1个部署阶段,则可以通过

$projects2.environments[0].deployPhases[0].deploymentInput.queueId = $id.id

If you have multiple environments and/or deploy phases you'll need to change the array keys on those properties accordingly. 如果您有多个环境和/或部署阶段,则需要相应地更改这些属性上的阵列键。 Then convert the $projects2 object to JSON before sending the HTTP PUT request: 然后在发送HTTP PUT请求之前将$ projects2对象转换为JSON:

$body = $projects2 | ConvertTo-Json -Depth 10
$uri3 = "https://vsrm.dev.azure.com/$vstsAccount/$resource/_apis/release/definitions?api-version=5.0"
Invoke-RestMethod -Method Put -Uri $uri3 -Headers @{Authorization=("Basic {0}" -f $base64authinfo)} -Body $body -ContentType application/json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 PowerShell 监控 VSTS 代理的可用性? - How to monitor VSTS agent availability using PowerShell? VSTS托管代理中的运行空间和作业发布了Azure Powershell脚本 - Runspaces and Jobs in VSTS hosted agent release Azure Powershell script Azure DevOps(VSTS)-在托管代理上使用Azure Az PowerShell - Azure DevOps (VSTS) - using Azure Az PowerShell on a Hosted Agent 如何使用PowerShell将服务主体分配给托管的VSTS发布管道中的AAD组? - How might I assign a service principal to an AAD group in my hosted VSTS release pipeline using PowerShell? 如何从PowerShell中以编程方式列出VSTS代理程序池? - How to list VSTS agent pools programmatically from PowerShell? VSTS发布AzureRM Powershell版本 - VSTS Release AzureRM Powershell Version 仅在脚本存在时如何在VSTS版本中运行远程Powershell? - How to run a remote Powershell in VSTS release only if script exists? 如何在单个代理程序池下的所有代理程序上运行Powershell脚本 - How to run a powershell script on all agents under single agent pool 如何使用sql job agent中的默认凭据运行powershell脚本 - How to run powershell script using default credential from sql job agent 如何在 Azure Devops Pipeline 中使用 PowerShell 根据条件触发代理作业 - How to trigger Agent Job As per condition using PowerShell in Azure Devops Pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM