Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit 035fb81

Browse files
niadakbishal-pdMSFT
authored andcommitted
Users/niadak/agent configuration script2 (#108)
* Adding agent re-configuration scripts for TFS hardware move scenario * updating the comments * adding example usage * Resolving comments * resolving comments 2 * Agent config script for detach attach scenario Agent config script for detach attach scenario * Adding script for the scenario when source collection is not online Adding script for the scenario when source collection is not online * Resolving comments Resolving comments
1 parent 10347d8 commit 035fb81

File tree

6 files changed

+288
-2
lines changed

6 files changed

+288
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<#
2+
This script is applicable for AzureDevOpsServer 2019 or later.
3+
This is a guidance script to re-configure deployment group agents to the target AzureDevOpsServer after detach/attach activity. This script is applicable if the source collection is not online. If source AzureDevOpsServer collection is online (Side by Side scenario), use ConfigureAgentToTargetServerUsingSourceServer,ps1 script instead.
4+
5+
Inputs:
6+
1. targetServerUrl : New AzureDevOpsServer instance url.
7+
2. targeServerPatToken : PAT token from target AzureDevOpsServer with Deployment group manage scope (at least)
8+
3. existingAgentFolder: The script will Auto-detect the agent folder if it was running as windows service. User need to pass the folder path otherwise.
9+
4. agentDownloadUrl: this is optional, user can specify if she wants any specific agent version to be installed.
10+
5. tags : Tags of the target. Since the source AzureDevOpsServer is not available, user need to pass the tags explicitly.
11+
5. action: By default, the script will not do any update operation, it will just print what changes will happen after the script is applied. User need to set action parameter to 'apply' to update execute actual steps.
12+
13+
Output:
14+
If action is set to apply, this script this script will
15+
1. Configure a new agent to the deployment group in the target AzureDevOpsServer with same properties as the existing agent in the source AzureDevOpsServer.
16+
Example usage:
17+
.\ConfigureAgentToTargetServer.ps1 -targetServerUrl <newAzureDevOpsServerUrl> -targeServerPatToken <PAT-for-new-AzureDevOpsServer>
18+
.\ConfigureAgentToTargetServer.ps1 -targetServerUrl <newAzureDevOpsServerUrl> -targeServerPatToken <PAT-for-new-AzureDevOpsServer> -existingAgentFolder <AgentFoilder (C:\vstsagents\A1)> -tags 'Web,DB'
19+
.\ConfigureAgentToTargetServer.ps1 -targetServerUrl <newAzureDevOpsServerUrl> -targeServerPatToken <PAT-for-new-AzureDevOpsServer> -existingAgentFolder <AgentFoilder (C:\vstsagents\A1)> -action 'apply'
20+
#>
21+
22+
param([string]$targetServerUrl,
23+
[string]$targeServerPatToken,
24+
[string]$existingAgentFolder = "",
25+
[string]$agentDownloadUrl = 'https://vstsagentpackage.azureedge.net/agent/2.141.1/vsts-agent-win-x64-2.141.1.zip',
26+
[string]$tags = "",
27+
[string]$action = "PrintEffect")
28+
29+
$ErrorActionPreference="Stop"
30+
$apiVersion = '5.0-preview.1'
31+
32+
# Basic validations
33+
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() ).IsInRole( [Security.Principal.WindowsBuiltInRole] Administrator)){
34+
throw "Run command in an administrator PowerShell prompt"
35+
};
36+
37+
If ($PSVersionTable.PSVersion -lt (New-Object System.Version("3.0"))){
38+
throw "The minimum version of Windows PowerShell that is required by the script (3.0) does not match the currently running version of Windows PowerShell."
39+
}
40+
41+
# Auto detect existing agent folder
42+
if ($existingAgentFolder -eq ""){
43+
$ServiceNamePrefix = 'vstsagent*'
44+
$agentServices = Get-Service -Name $ServiceNamePrefix
45+
if ($agentServices.Count -eq 1){
46+
$serviceName = $agentServices[0].Name;
47+
$service = get-wmiobject -query "select * from win32_service where name = `'$serviceName`'";
48+
$serviceExePath = $service.pathname;
49+
# $serviceExePath will have value like "C:\vstsAgent2\A8\bin\AgentService.exe"
50+
$existingAgentFolder = Split-Path (Split-Path $serviceExePath -Parent) -Parent;
51+
}
52+
}
53+
54+
if ($existingAgentFolder -eq ""){
55+
throw "Not able to auto detect existing agent folder. Provide the existingAgentFolder as input parameter. You can find it in agent capabilities UI as Agent.HomeDirectory. Generally it is in C:\vstsagent folder.";
56+
}
57+
58+
cd $existingAgentFolder;
59+
60+
if (-not (Test-Path '.\.agent')){
61+
throw "No agent installed in this path. Please run this script from Agent Home Directory. Generally it is in C:\vstsagent folder."
62+
}
63+
64+
65+
# Collect information about the existing agent
66+
$sourceServerUrl = "NoAbleToReadAgentFile"
67+
$collectionName = "NoAbleToReadAgentFile"
68+
$projectName = "NoAbleToReadAgentFile"
69+
$deploymentGroupId = "NoAbleToReadAgentFile"
70+
$deploymentGroupName = "NoAbleToReadAgentFile"
71+
$agentName = "NoAbleToReadAgentFile"
72+
73+
$agentproperties = Get-Content .\.agent |ConvertFrom-Json
74+
$sourceServerUrl = $agentproperties.serverUrl;
75+
$collectionName = $agentproperties.collectionName;
76+
$projectName = $agentproperties.projectId;
77+
$deploymentGroupId = $agentproperties.deploymentGroupId;
78+
$agentName = $agentproperties.agentName;
79+
80+
if ($sourceServerUrl.StartsWith($targetServerUrl)){
81+
Write-Verbose -Verbose "Agent $agentName is already configured to the AzureDevOpsServer $targetServerUrl";
82+
return 0;
83+
}
84+
85+
# Get the Deployment group name
86+
$encodedPat = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$targeServerPatToken"))
87+
$getDeploymentGroupUrl = $targetServerUrl + '/' + $collectionName + '/' + $projectName + '/_apis/distributedtask/deploymentgroups/' + $deploymentGroupId + '?api-version=' + $apiVersion
88+
89+
$dg = Invoke-RestMethod -Uri $getDeploymentGroupUrl -Method Get -Headers @{Authorization = "Basic $encodedPat"} -ContentType "application/json"
90+
$deploymentGroupName = $dg.name
91+
92+
93+
# Create and go to new agent folder
94+
$parentPath = Split-Path $existingAgentFolder -Parent
95+
cd $parentPath
96+
$newAgentFolfer = "";
97+
98+
for ($i=1; $i -lt 100; $i++){
99+
$destFolder="A"+$i.ToString();
100+
if (-NOT (Test-Path ($destFolder))){
101+
$newAgentFolfer = $destFolder;
102+
break;
103+
}
104+
};
105+
106+
$newAgentPath = $parentPath + $newAgentFolfer;
107+
108+
if ($action -ne "apply"){
109+
Write-Verbose -Verbose "If action is set to apply, this script will configure an agent with name $agentName and tags $tags in $newAgentPath path to the deployment group $deploymentGroupName in the AzureDevOpsServer $targetServerUrl with same properties as the existing agent $agentName in the source AzureDevOpsServer $sourceServerUrl.";
110+
return 0;
111+
}
112+
113+
Write-Verbose -Verbose "Start execution : It will configure an agent with name $agentName and tags $tags in $newAgentPath path to the deployment group $deploymentGroupName in the AzureDevOpsServer $targetServerUrl with same properties as the existing agent $agentName in the source AzureDevOpsServer $sourceServerUrl.";
114+
mkdir $newAgentFolfer;
115+
cd $newAgentFolfer;
116+
117+
# download the agent bits.
118+
$agentZip= $agentZip="$PWD\agent.zip";
119+
$DefaultProxy=[System.Net.WebRequest]::DefaultWebProxy;$securityProtocol=@();
120+
$securityProtocol+=[Net.ServicePointManager]::SecurityProtocol;
121+
$securityProtocol+=[Net.SecurityProtocolType]::Tls12;[Net.ServicePointManager]::SecurityProtocol=$securityProtocol;
122+
$WebClient=New-Object Net.WebClient;
123+
124+
if($DefaultProxy -and (-not $DefaultProxy.IsBypassed($agentDownloadUrl)))
125+
{
126+
$WebClient.Proxy= New-Object Net.WebProxy($DefaultProxy.GetProxy($agentDownloadUrl).OriginalString, $True);
127+
};
128+
129+
$WebClient.DownloadFile($agentDownloadUrl, $agentZip);
130+
131+
Add-Type -AssemblyName System.IO.Compression.FileSystem;[System.IO.Compression.ZipFile]::ExtractToDirectory( $agentZip, "$PWD");
132+
133+
134+
# configure the agent
135+
if ($tags -ne ""){
136+
.\config.cmd --deploymentgroup --url $targetServerUrl --collectionname $collectionName --projectname $projectName --deploymentgroupname $deploymentGroupName --agent $agentName --auth Integrated --runasservice --work '_work' --unattended --adddeploymentgrouptags --deploymentgrouptags $tags
137+
}
138+
else{
139+
.\config.cmd --deploymentgroup --url $targetServerUrl --collectionname $collectionName --projectname $projectName --deploymentgroupname $deploymentGroupName --agent $agentName --auth Integrated --runasservice --work '_work' --unattended
140+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<#
2+
This script is applicable for AzureDevOpsServer 2019 or later.
3+
This is a guidance script to re-configure deployment group agents to the target AzureDevOpsServer after detach/attach activity. The script is applicable if both source and target AzureDevOpsServer collections are online.
4+
5+
Inputs:
6+
1. targetServerUrl : New AzureDevOpsServer instance url.
7+
2. sourceServerPatToken : PAT token from source AzureDevOpsServer with Deployment group manage scope (at least)
8+
3. existingAgentFolder: The script will Auto-detect the agent folder if it was running as windows service. User need to pass the folder path otherwise.
9+
4. agentDownloadUrl: this is optional, user can specify if she wants any specific agent version to be installed.
10+
5. action: By default, the script will not do any update operation, it will just print what changes will happen after the script is applied. User need to set action parameter to 'apply' to update execute actual steps.
11+
12+
Output:
13+
If action is set to apply, this script this script will
14+
1. Configure a new agent to the deployment group in the target AzureDevOpsServer with same properties as the existing agent in the source AzureDevOpsServer including tags.
15+
Example usage:
16+
.\ConfigureAgentToTargetServerUsingSourceServer.ps1 -targetServerUrl <newAzureDevOpsServerUrl> -sourceServerPatToken <PAT-for-old-AzureDevOpsServer>
17+
.\ConfigureAgentToTargetServerUsingSourceServer.ps1 -targetServerUrl <newAzureDevOpsServerUrl> -sourceServerPatToken <PAT-for-old-AzureDevOpsServer> -existingAgentFolder <AgentFoilder (C:\vstsagents\A1)>
18+
.\ConfigureAgentToTargetServerUsingSourceServer.ps1 -targetServerUrl <newAzureDevOpsServerUrl> -sourceServerPatToken <PAT-for-old-AzureDevOpsServer> -existingAgentFolder <AgentFoilder (C:\vstsagents\A1)> -action 'apply'
19+
#>
20+
21+
param([string]$targetServerUrl,
22+
[string]$sourceServerPatToken,
23+
[string]$existingAgentFolder = "",
24+
[string]$agentDownloadUrl = 'https://vstsagentpackage.azureedge.net/agent/2.141.1/vsts-agent-win-x64-2.141.1.zip',
25+
[string]$action = "PrintEffect")
26+
27+
$ErrorActionPreference="Stop"
28+
$apiVersion = '5.0-preview.1'
29+
30+
# Basic validations
31+
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() ).IsInRole( [Security.Principal.WindowsBuiltInRole] Administrator)){
32+
throw "Run command in an administrator PowerShell prompt"
33+
};
34+
35+
If ($PSVersionTable.PSVersion -lt (New-Object System.Version("3.0"))){
36+
throw "The minimum version of Windows PowerShell that is required by the script (3.0) does not match the currently running version of Windows PowerShell."
37+
}
38+
39+
# Auto detect existing agent folder
40+
if ($existingAgentFolder -eq ""){
41+
$ServiceNamePrefix = 'vstsagent*'
42+
$agentServices = Get-Service -Name $ServiceNamePrefix
43+
if ($agentServices.Count -eq 1){
44+
$serviceName = $agentServices[0].Name;
45+
$service = get-wmiobject -query "select * from win32_service where name = `'$serviceName`'";
46+
$serviceExePath = $service.pathname;
47+
# $serviceExePath will have value like "C:\vstsAgent2\A8\bin\AgentService.exe"
48+
$existingAgentFolder = Split-Path (Split-Path $serviceExePath -Parent) -Parent;
49+
}
50+
}
51+
52+
if ($existingAgentFolder -eq ""){
53+
throw "Not able to auto detect existing agent folder. Provide the existingAgentFolder as input parameter. You can find it in agent capabilities UI as Agent.HomeDirectory. Generally it is in C:\vstsagent folder.";
54+
}
55+
56+
cd $existingAgentFolder;
57+
58+
if (-not (Test-Path '.\.agent')){
59+
throw "No agent installed in this path. Please run this script from Agent Home Directory. Generally it is in C:\vstsagent folder."
60+
}
61+
62+
63+
# Collect information about the existing agent
64+
$sourceServerUrl = "NoAbleToReadAgentFile"
65+
$collectionName = "NoAbleToReadAgentFile"
66+
$projectName = "NoAbleToReadAgentFile"
67+
$deploymentGroupId = "NoAbleToReadAgentFile"
68+
$deploymentGroupName = "NoAbleToReadAgentFile"
69+
$agentName = "NoAbleToReadAgentFile"
70+
$tags = "NoAbleToReadAgentFile"
71+
72+
$agentproperties = Get-Content .\.agent |ConvertFrom-Json
73+
$sourceServerUrl = $agentproperties.serverUrl;
74+
$collectionName = $agentproperties.collectionName;
75+
$projectName = $agentproperties.projectId;
76+
$deploymentGroupId = $agentproperties.deploymentGroupId;
77+
$agentName = $agentproperties.agentName;
78+
79+
if ($sourceServerUrl.StartsWith($targetServerUrl)){
80+
Write-Verbose -Verbose "Agent $agentName is already configured to the AzureDevOpsServer $targetServerUrl";
81+
return 0;
82+
}
83+
84+
# Get the Deployment group name
85+
$encodedPat = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$sourceServerPatToken"))
86+
$getDeploymentGroupUrl = $sourceServerUrl + '/' + $collectionName + '/' + $projectName + '/_apis/distributedtask/deploymentgroups/' + $deploymentGroupId + '?api-version=' + $apiVersion
87+
88+
$dg = Invoke-RestMethod -Uri $getDeploymentGroupUrl -Method Get -Headers @{Authorization = "Basic $encodedPat"} -ContentType "application/json"
89+
$deploymentGroupName = $dg.name
90+
91+
92+
# Get the Machine Tags
93+
$getDeploymentMachineUrl = $sourceServerUrl + '/' + $collectionName + '/' + $projectName + '/_apis/distributedtask/deploymentgroups/' + $deploymentGroupId + '/targets?name=' + $agentName + '&api-version=' + $apiVersion
94+
95+
$dm = Invoke-RestMethod -Uri $getDeploymentMachineUrl -Method Get -Headers @{Authorization = "Basic $encodedPat"} -ContentType "application/json"
96+
$tags = $dm.value[0].tags -join ",";
97+
98+
99+
# Create and go to new agent folder
100+
$parentPath = Split-Path $existingAgentFolder -Parent
101+
cd $parentPath
102+
$newAgentFolfer = "";
103+
104+
for ($i=1; $i -lt 100; $i++){
105+
$destFolder="A"+$i.ToString();
106+
if (-NOT (Test-Path ($destFolder))){
107+
$newAgentFolfer = $destFolder;
108+
break;
109+
}
110+
};
111+
112+
$newAgentPath = $parentPath + $newAgentFolfer;
113+
114+
if ($action -ne "apply"){
115+
Write-Verbose -Verbose "If action is set to apply, this script will configure an agent with name $agentName in $newAgentPath path to the deployment group $deploymentGroupName in the AzureDevOpsServer $targetServerUrl with same properties as the existing agent $agentName in the source AzureDevOpsServer $sourceServerUrl incluing tags $tags.";
116+
return 0;
117+
}
118+
119+
Write-Verbose -Verbose "Start execution : It will configure an agent with name $agentName in $newAgentPath path to the deployment group $deploymentGroupName in the AzureDevOpsServer $targetServerUrl with same properties as the existing agent $agentName in the source AzureDevOpsServer $sourceServerUrl incluing tags $tags.";
120+
mkdir $newAgentFolfer;
121+
cd $newAgentFolfer;
122+
123+
# download the agent bits.
124+
$agentZip= $agentZip="$PWD\agent.zip";
125+
$DefaultProxy=[System.Net.WebRequest]::DefaultWebProxy;$securityProtocol=@();
126+
$securityProtocol+=[Net.ServicePointManager]::SecurityProtocol;
127+
$securityProtocol+=[Net.SecurityProtocolType]::Tls12;[Net.ServicePointManager]::SecurityProtocol=$securityProtocol;
128+
$WebClient=New-Object Net.WebClient;
129+
130+
if($DefaultProxy -and (-not $DefaultProxy.IsBypassed($agentDownloadUrl)))
131+
{
132+
$WebClient.Proxy= New-Object Net.WebProxy($DefaultProxy.GetProxy($agentDownloadUrl).OriginalString, $True);
133+
};
134+
135+
$WebClient.DownloadFile($agentDownloadUrl, $agentZip);
136+
137+
Add-Type -AssemblyName System.IO.Compression.FileSystem;[System.IO.Compression.ZipFile]::ExtractToDirectory( $agentZip, "$PWD");
138+
139+
140+
# configure the agent
141+
if ($tags -ne ""){
142+
.\config.cmd --deploymentgroup --url $targetServerUrl --collectionname $collectionName --projectname $projectName --deploymentgroupname $deploymentGroupName --agent $agentName --auth Integrated --runasservice --work '_work' --unattended --adddeploymentgrouptags --deploymentgrouptags $tags
143+
}
144+
else{
145+
.\config.cmd --deploymentgroup --url $targetServerUrl --collectionname $collectionName --projectname $projectName --deploymentgroupname $deploymentGroupName --agent $agentName --auth Integrated --runasservice --work '_work' --unattended
146+
}

DeploymentAgentReconfigurationScripts/TFS2018 Update2/HardwareMoveRemoveOldAgent.ps1 renamed to DeploymentAgentReconfigurationScripts/HardwareMoveScanrio/TFS2018 Update2/HardwareMoveRemoveOldAgent.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<#
2-
This script is applicable for TFS 2018 Update2.
2+
This script is applicable for TFS 2018 Update2 or later.
33
This is a guidance script to bring a deployment group agent online after TFS Hardware move activity. Post Hardware move, all the agents will be offline as the TFS server url has been updated. User can use\follow this script to bring them online. The script has to be run from Administrator PowerShell prompt on the each agent machine.
44
55
Inputs:

DeploymentAgentReconfigurationScripts/TFS2018 Update2/HardwareMoveRetainOldAgent.ps1 renamed to DeploymentAgentReconfigurationScripts/HardwareMoveScanrio/TFS2018 Update2/HardwareMoveRetainOldAgent.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<#
2-
This script is applicable for TFS 2018 Update2.
2+
This script is applicable for TFS 2018 Update2 or later.
33
This is a guidance script to bring a deployment group agent online after TFS Hardware move activity. Post Hardware move, all the agents will be offline as the TFS server url has been updated. User can use\follow this script to bring them online. The script has to be run from Administrator PowerShell prompt on the each agent machine.
44
55
Inputs:

0 commit comments

Comments
 (0)