Skip to content

Commit 96dbe2b

Browse files
Repository, Endpoint, and AgentPool Commands. New Parts to support them.
1 parent 4f9332f commit 96dbe2b

9 files changed

+1081
-0
lines changed

Get-ADOAgentPool.ps1

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
function Get-ADOAgentPool
2+
{
3+
<#
4+
.Synopsis
5+
Gets Azure DevOps Agent Pools
6+
.Description
7+
Gets Agent Pools and their associated queues from Azure DevOps.
8+
9+
Queues associate a given project with a pool.
10+
Pools are shared by organization.
11+
12+
Thus providing a project will return the queues associated with the project,
13+
and just providing the organization will return all of the common pools.
14+
.Example
15+
Get-ADOAgentPool -Organization MyOrganization -PersonalAccessToken $pat
16+
.Link
17+
https://docs.microsoft.com/en-us/rest/api/azure/devops/distributedtask/pools/get%20agent%20pools?view=azure-devops-rest-5.1
18+
.Link
19+
https://docs.microsoft.com/en-us/rest/api/azure/devops/distributedtask/queues/get%20agent%20queues?view=azure-devops-rest-5.1
20+
#>
21+
[CmdletBinding(DefaultParameterSetName='distributedtask/pools')]
22+
[OutputType('StartAutomating.PSDevops.Pool')]
23+
param(
24+
# The Organization
25+
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
26+
[Alias('Org')]
27+
[string]
28+
$Organization,
29+
30+
# The project name or identifier.
31+
[Parameter(Mandatory,ParameterSetName='distributedtask/queues',ValueFromPipelineByPropertyName)]
32+
[Alias('ProjectID')]
33+
[string]
34+
$Project,
35+
36+
# The server. By default https://dev.azure.com/.
37+
# To use against TFS, provide the tfs server URL (e.g. http://tfsserver:8080/tfs).
38+
[Parameter(ValueFromPipelineByPropertyName)]
39+
[uri]
40+
$Server = "https://dev.azure.com/",
41+
42+
# The api version. By default, 5.1.
43+
# If targeting TFS, this will need to change to match your server version.
44+
# See: https://docs.microsoft.com/en-us/azure/devops/integrate/concepts/rest-api-versioning?view=azure-devops
45+
[string]
46+
$ApiVersion = "5.1-preview",
47+
48+
# A Personal Access Token
49+
[Alias('PAT')]
50+
[string]
51+
$PersonalAccessToken,
52+
53+
# Specifies a user account that has permission to send the request. The default is the current user.
54+
# Type a user name, such as User01 or Domain01\User01, or enter a PSCredential object, such as one generated by the Get-Credential cmdlet.
55+
[pscredential]
56+
[Management.Automation.CredentialAttribute()]
57+
$Credential,
58+
59+
# Indicates that the cmdlet uses the credentials of the current user to send the web request.
60+
[Alias('UseDefaultCredential')]
61+
[switch]
62+
$UseDefaultCredentials,
63+
64+
# Specifies that the cmdlet uses a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server.
65+
[uri]
66+
$Proxy,
67+
68+
# Specifies a user account that has permission to use the proxy server that is specified by the Proxy parameter. The default is the current user.
69+
# Type a user name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one generated by the Get-Credential cmdlet.
70+
# This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the ProxyCredential and ProxyUseDefaultCredentials parameters in the same command.
71+
[pscredential]
72+
[Management.Automation.CredentialAttribute()]
73+
$ProxyCredential,
74+
75+
# Indicates that the cmdlet uses the credentials of the current user to access the proxy server that is specified by the Proxy parameter.
76+
# This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the ProxyCredential and ProxyUseDefaultCredentials parameters in the same command.
77+
[switch]
78+
$ProxyUseDefaultCredentials
79+
)
80+
81+
begin {
82+
#region Copy Invoke-ADORestAPI parameters
83+
$invokeParams = & $getInvokeParameters $PSBoundParameters
84+
#endregion Copy Invoke-ADORestAPI parameters
85+
}
86+
87+
process {
88+
$uri = # The URI is comprised of:
89+
@(
90+
"$server".TrimEnd('/') # the Server (minus any trailing slashes),
91+
$Organization # the Organization,
92+
if ($Project) {$project} # the Project (if present),
93+
'_apis' # the API Root ('_apis'),
94+
(. $ReplaceRouteParameter $PSCmdlet.ParameterSetName)
95+
# and any parameterized URLs in this parameter set.
96+
) -as [string[]] -ne '' -join '/'
97+
98+
$uri += '?' # The URI has a query string containing:
99+
$uri += @(
100+
if ($ApiVersion) { # the api-version
101+
"api-version=$apiVersion"
102+
}
103+
) -join '&'
104+
105+
# We want to decorate our return value. Handily enough, both URIs contain a distinct name in the last URL segment.
106+
$typename = @($psCmdlet.ParameterSetName -split '/')[-1].TrimEnd('s') # We just need to drop the 's'
107+
$typeNames = @(
108+
"$organization.$typename"
109+
if ($Project) { "$organization.$Project.$typename" }
110+
"StartAutomating.PSDevOps.$typename"
111+
)
112+
113+
Invoke-ADORestAPI -Uri $uri @invokeParams -PSTypeName $typenames
114+
}
115+
}

Get-ADORepository.ps1

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
function Get-ADORepository
2+
{
3+
<#
4+
.Synopsis
5+
Gets repositories from Azure DevOps
6+
.Description
7+
Gets the repositories from Azure DevOps.
8+
9+
By default, this will return the project's git repositories.
10+
11+
Azure DevOps repositories can have more than one type of SourceProvider.
12+
13+
To list the Source Providers, use -SourceProvider
14+
15+
We can get repositories for a given -ProviderName
16+
.Example
17+
Get-ADORepository -Organization StartAutomating -Project PSDevOps
18+
.Link
19+
Remove-ADORepository
20+
.Link
21+
https://docs.microsoft.com/en-us/rest/api/azure/devops/build/source%20providers/list%20repositories?view=azure-devops-rest-5.1
22+
#>
23+
[CmdletBinding(DefaultParameterSetName='git/repositories')]
24+
param(
25+
# The Organization
26+
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
27+
[Alias('Org')]
28+
[string]
29+
$Organization,
30+
31+
# The Project
32+
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
33+
[string]
34+
$Project,
35+
36+
# The Repository ID
37+
[Parameter(Mandatory,ParameterSetName='git/repositories/{repositoryId}',ValueFromPipelineByPropertyName)]
38+
[string]
39+
$RepositoryID,
40+
41+
# If set, will list repository source providers
42+
[Parameter(Mandatory,ParameterSetName='sourceproviders',ValueFromPipelineByPropertyName)]
43+
[Alias('SourceProviders')]
44+
[switch]
45+
$SourceProvider,
46+
47+
# The name of the Source Provider. This will get all repositories associated with the project.
48+
# If the -ProviderName is not TFVC or TFGit, an -EndpointID is also required
49+
[Parameter(Mandatory,ParameterSetName='sourceproviders/{ProviderName}/repositories',ValueFromPipelineByPropertyName)]
50+
[Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)]
51+
[Alias('EndpointType')]
52+
[string]
53+
$ProviderName,
54+
55+
# The name of the Source Provider. This will get all repositories associated with the project.
56+
# If the -ProviderName is not TFVC or TFGit, an -EndpointID is also required
57+
[Parameter(ParameterSetName='sourceproviders/{ProviderName}/repositories',ValueFromPipelineByPropertyName)]
58+
[Parameter(ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)]
59+
[string]
60+
$EndpointID,
61+
62+
# The name of the repository
63+
[Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)]
64+
[string]
65+
$RepositoryName,
66+
67+
# The path within the repository.
68+
# To use this parameter, -ProviderName is also required, and -EndpointID will be required if the -ProviderName is not TFVC or TFGit
69+
[Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)]
70+
[string]
71+
$Path,
72+
73+
# The commit or branch. By default, Master.
74+
[Parameter(ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)]
75+
[string]
76+
$CommitOrBranch = 'master',
77+
78+
# If set, will include the parent repository
79+
[Parameter(ParameterSetName='git/repositories/{repositoryId}',ValueFromPipelineByPropertyName)]
80+
[switch]
81+
$IncludeParent,
82+
83+
# If set, will get repositories from the recycle bin
84+
[Parameter(Mandatory,ParameterSetName='git/recycleBin/repositories',ValueFromPipelineByPropertyName)]
85+
[Alias('RecycleBin')]
86+
[switch]
87+
$Recycled,
88+
89+
# The server. By default https://dev.azure.com/.
90+
# To use against TFS, provide the tfs server URL (e.g. http://tfsserver:8080/tfs).
91+
[Parameter(ValueFromPipelineByPropertyName)]
92+
[uri]
93+
$Server = "https://dev.azure.com/",
94+
95+
# The api version. By default, 5.1-preview.
96+
# If targeting TFS, this will need to change to match your server version.
97+
# See: https://docs.microsoft.com/en-us/azure/devops/integrate/concepts/rest-api-versioning?view=azure-devops
98+
[string]
99+
$ApiVersion = "5.1-preview",
100+
101+
# A Personal Access Token
102+
[Alias('PAT')]
103+
[string]
104+
$PersonalAccessToken,
105+
106+
# Specifies a user account that has permission to send the request. The default is the current user.
107+
# Type a user name, such as User01 or Domain01\User01, or enter a PSCredential object, such as one generated by the Get-Credential cmdlet.
108+
[pscredential]
109+
[Management.Automation.CredentialAttribute()]
110+
$Credential,
111+
112+
# Indicates that the cmdlet uses the credentials of the current user to send the web request.
113+
[Alias('UseDefaultCredential')]
114+
[switch]
115+
$UseDefaultCredentials,
116+
117+
# Specifies that the cmdlet uses a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server.
118+
[uri]
119+
$Proxy,
120+
121+
# Specifies a user account that has permission to use the proxy server that is specified by the Proxy parameter. The default is the current user.
122+
# Type a user name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one generated by the Get-Credential cmdlet.
123+
# This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the ProxyCredential and ProxyUseDefaultCredentials parameters in the same command.
124+
[pscredential]
125+
[Management.Automation.CredentialAttribute()]
126+
$ProxyCredential,
127+
128+
# Indicates that the cmdlet uses the credentials of the current user to access the proxy server that is specified by the Proxy parameter.
129+
# This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the ProxyCredential and ProxyUseDefaultCredentials parameters in the same command.
130+
[switch]
131+
$ProxyUseDefaultCredentials
132+
)
133+
134+
begin {
135+
#region Copy Invoke-ADORestAPI parameters
136+
$invokeParams = & $getInvokeParameters $PSBoundParameters
137+
#endregion Copy Invoke-ADORestAPI parameters
138+
}
139+
140+
process {
141+
$uri = # The URI is comprised of:
142+
@(
143+
"$server".TrimEnd('/') # the Server (minus any trailing slashes),
144+
$Organization # the Organization,
145+
$Project # the Project,
146+
'_apis' # the API Root ('_apis'),
147+
(. $ReplaceRouteParameter $PSCmdlet.ParameterSetName)
148+
# and any parameterized URLs in this parameter set.
149+
) -as [string[]] -ne '' -join '/'
150+
$uri += '?' # The URI has a query string containing:
151+
$uri += @(
152+
if ($IncludeParent) { # includeParent=True (if it was set)
153+
"includeParent=True"
154+
}
155+
156+
if ($EndpointID) {
157+
"serviceEndpointId=$EndpointID"
158+
}
159+
160+
if ($repositoryName) {
161+
"repository=$repositoryName"
162+
"commitOrBranch=$CommitOrBranch"
163+
}
164+
165+
if ($path) {
166+
"path=$path"
167+
}
168+
169+
if ($ApiVersion) { # and the apiVersion
170+
"api-version=$ApiVersion"
171+
}
172+
) -join '&'
173+
174+
$InvokeParams.Property =
175+
@{
176+
# Because we want to pipeline properly, also return the -Organization and -Project as properties.
177+
Organization = $Organization
178+
Project = $Project
179+
}
180+
181+
$subTypeName =
182+
if ($psCmdlet.ParameterSetName -eq 'git/recycleBin/repositories') {
183+
'.Recycled'
184+
}
185+
elseif ($psCmdlet.ParameterSetName -eq 'sourceProviders') {
186+
'.SourceProvider'
187+
}
188+
elseif ($psCmdlet.ParameterSetName -eq 'sourceproviders/{ProviderName}/repositories') {
189+
".$ProviderName.Repository"
190+
$invokeParams.ExpandProperty = 'repositories'
191+
$invokeParams.Property.EndpointID = $EndpointID
192+
$invokeParams.Property.ProviderName = $ProviderName
193+
}
194+
else {
195+
''
196+
}
197+
198+
# Invoke the ADO Rest API.
199+
Invoke-ADORestAPI @invokeParams -Uri $uri -PSTypeName @(
200+
# Because we want to format the output, decorate the object with the following typenames:
201+
"$Organization.$Project.Repository$subTypeName" # * $Organization.$Project.Repository$SubTypeName
202+
"$Organization.Repository$subTypeName" # * $Organization.Repository$SubTypeName
203+
"StartAutomating.PSDevOps.Repository$subTypeName" # * PSDevOps.Repository$SubTypeName
204+
)
205+
}
206+
}

0 commit comments

Comments
 (0)