forked from StartAutomating/PSDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connect-ADO.ps1
163 lines (143 loc) · 5.86 KB
/
Connect-ADO.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function Connect-ADO
{
<#
.Synopsis
Connects to Azure DeVOps
.Description
Connects the current PowerShell session to Azure DeVOps or a Team Foundation Server.
Information passed to Connect-ADO will be used as the default parameters to all -ADO* commands from PSDevOps.
PersonalAccessTokens will be cached separately to improve security.
.Example
Connect-ADO -Organization StartAutomating -PersonalAccessToken $myPat
.Link
Disconnect-ADO
#>
[OutputType('PSDevOps.Connection')]
param(
# The organization.
# When connecting to TFS, this is the Project Collection.
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias('Org')]
[string]
$Organization,
# The Personal Access Token.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('PAT')]
[string]
$PersonalAccessToken,
# If set, will use default credentials when connecting.
[Parameter(ValueFromPipelineByPropertyName)]
[switch]
$UseDefaultCredentials,
# The credential used to connect.
[Parameter(ValueFromPipelineByPropertyName)]
[Management.Automation.PSCredential]
$Credential,
# The Server. If this points to a TFS server, it should be the root TFS url, i.e. http://localhost:8080/tfs
[Parameter(ValueFromPipelineByPropertyName)]
[uri]
$Server
)
begin {
$myModuleCommands =
@(if ($MyInvocation.MyCommand.ScriptBlock.Module) {
$MyInvocation.MyCommand.ScriptBlock.Module.ExportedCommands.Values
})
}
process {
if (-not $PSDefaultParameterValues) {
Write-Error "PSDefaultParameterValues not found"
return
}
else {
Disconnect-ADO -Confirm:$false
}
$toCache = [Ordered]@{} + $PSBoundParameters
$toCache.Remove('PersonalAccessToken')
#region Set PSDefaultParameterValues
foreach ($cmd in $myModuleCommands) {
if ($cmd.Name -notlike '*-ADO*') { continue } # If its not an Azure DevOps command, skip it.
# Walk over each parameter we're caching.
foreach ($kv in $toCache.GetEnumerator()) {
$paramExists = $cmd.Parameters.($kv.Key) # Check to see if the parameter exists
if (-not $paramExists) { # if it didn't, check the aliases.
foreach ($v in $cmd.Parameters.Values) {
if ($v.Aliases -contains $kv.Key) {
$paramExists = $cmd.Parameters.($v.Name)
}
}
}
if ($paramExists) { # If the parameter existed, set $global:PSDefaultParameterValues
$global:PSDefaultParameterValues["${cmd}:$($kv.Key)"] = $kv.Value
}
}
}
#endregion Set PSDefaultParameterValues
#region Cache PersonalAccessToken
if ($PersonalAccessToken) {
$Script:CachedPersonalAccessToken = $PersonalAccessToken
$getProjects = @(Get-ADOProject @PSBoundParameters)
$getMyTeams = @(Get-ADOTeam @PSBoundParameters -Mine)
if (-not ($getMyTeams -or $getProjects)) {
Disconnect-ADO
return
}
}
#endregion Cache PersonalAccessToken
#region Cache and Output Connection
$script:ADOConnectionInfo = $toCache
$output = [PSCustomObject]$toCache
$output.pstypenames.clear()
$output.pstypenames.add("$Organization.Connection")
$output.pstypenames.add('PSDevOps.Connection')
$output
#endregion Cache and Output Connection
$registerArgumentCompleter =
$ExecutionContext.SessionState.InvokeCommand.GetCommand('Register-ArgumentCompleter','Cmdlet')
if ($registerArgumentCompleter) {
$unique = @{
Project = @(
$getMyTeams | Select-Object -ExpandProperty ProjectName
$getProjects | Select-Object -ExpandProperty Name
) |
Select-Object -Unique
Team = $getMyTeams | Select-Object -ExpandProperty Team -Unique
TeamID = $getMyTeams | Select-Object -ExpandProperty TeamID -Unique
ProjectID =
$getProjects + $getMyTeams |
Select-Object -ExpandProperty ProjectID -Unique
}
$uniqueCompleter = @{}
foreach ($u in $unique.GetEnumerator()) {
$quotedValues = @(foreach ($v in $u.Value) {
if ($v.Contains(' ')) {
"'''$v'''"
}
else {
"'$v'"
}
}) | Sort-Object
if (-not $quotedValues) { continue }
$uniqueCompleter[$u.Key] = [ScriptBlock]::Create(
'param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)' +
"$($quotedValues -join ",") | Where-Object { `$_ -like `"`$wordToComplete*`"}"
)
}
$uniqueCommands = @{}
foreach ($u in $unique.GetEnumerator()) {
$uniqueCommands[$u.Key] = @()
}
foreach ($cmd in $myModuleCommands) {
if ($cmd.Name -notlike '*-ADO*') { continue }
foreach ($u in $unique.Keys) {
if (-not $cmd.Parameters.($u)) { continue }
$uniqueCommands[$u]+=$cmd.Name
}
}
foreach ($u in $uniqueCommands.Keys) {
if (-not $uniqueCompleter[$u]) { continue }
& $registerArgumentCompleter -CommandName $uniqueCommands[$u] -ParameterName $u -ScriptBlock $uniqueCompleter[$u]
}
}
}
}