Skip to content

Commit b8c184e

Browse files
Adding Register-ADOArtifactFeed
1 parent 5171568 commit b8c184e

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

Register-ADOArtifactFeed.ps1

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
function Register-ADOArtifactFeed
2+
{
3+
<#
4+
.Synopsis
5+
Registers an Azure DevOps artifact feed.
6+
.Description
7+
Registers an Azure DevOps artifact feed as a PowerShell Repository.
8+
thThis allows Install-Module, Publish-Module, and Save-Module to work against an Azure DevOps artifact feed.
9+
.Link
10+
https://docs.microsoft.com/en-us/azure/devops/artifacts/tutorials/private-powershell-library?view=azure-devops
11+
.Link
12+
Get-ADOArtifactFeed
13+
.Link
14+
Unregister-ADOArtifactFeed
15+
.Link
16+
Get-PSRepository
17+
.Link
18+
Register-PSRepository
19+
.Link
20+
Unregister-PSRepository
21+
#>
22+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "",
23+
Justification="Abstracting Credential Structure is part of the point")]
24+
param(
25+
# The name of the organization.
26+
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
27+
[string]
28+
$Organization,
29+
30+
# The Project
31+
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
32+
[string]
33+
$Project,
34+
35+
# The name or ID of the feed.
36+
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
37+
[Alias('fullyQualifiedId')]
38+
[string]
39+
$FeedID,
40+
41+
# The personal access token used to connect to the feed.
42+
[Parameter(ValueFromPipelineByPropertyName)]
43+
[Alias('PAT')]
44+
[string]
45+
$PersonalAccessToken,
46+
47+
# The email address used to connect
48+
[Parameter(ValueFromPipelineByPropertyName)]
49+
[string]
50+
$EmailAddress,
51+
52+
# If provided, will create a repository source using a given name.
53+
# By default, the RepositoryName will be $Organization-$Project-$FeedID
54+
[Parameter(ValueFromPipelineByPropertyName)]
55+
[string]
56+
$RepositoryName,
57+
58+
# If provided, will create a repository using a given URL.
59+
# By default, the RepositoryURL is predicted using -Organization, -Project, and -FeedID
60+
[Parameter(ValueFromPipelineByPropertyName)]
61+
[string]
62+
$RepositoryUrl,
63+
64+
# If set, will remove the connection to an existing feed and then create a new one.
65+
[switch]
66+
$Force
67+
)
68+
69+
70+
begin {
71+
$psRepos = Get-PSRepository
72+
}
73+
process {
74+
#region Check if Repository Already Exists
75+
$targetName = if ($RepositoryName) { $RepositoryName }
76+
else { "${Organization}-${Project}-${FeedID}" }
77+
$targetSource = if ($RepositoryUrl) { $RepositoryUrl }
78+
else { "https://pkgs.dev.azure.com/$Organization/$Project/_packaging/$FeedID/nuget/v2" }
79+
$psRepoExists = $psRepos |
80+
Where-Object {
81+
$_.Name -eq $targetName -or
82+
$_.SourceLocation -eq $targetSource
83+
}
84+
85+
if ($psRepoExists -and $Force) {
86+
$psRepoExists | Unregister-PSRepository
87+
} elseif ($psRepoExists) {
88+
Write-Verbose "Repository already exists: $($psRepoExists.Name)"
89+
return $psRepoExists
90+
}
91+
#endregion Check if Repository Already Exists
92+
93+
#region Create Credential and Register-PSRepository
94+
if (-not $PersonalAccessToken -and $env:SYSTEM_ACCESSTOKEN) {
95+
$PersonalAccessToken = $env:SYSTEM_ACCESSTOKEN
96+
}
97+
98+
if (-not $EmailAddress -and $PersonalAccessToken) {
99+
$EmailAddress = $PersonalAccessToken
100+
}
101+
102+
if (-not $EmailAddress -and -not $PersonalAccessToken) {
103+
Write-Error "Must provide a -PersonalAccessToken. Should provide an -EmailAddress"
104+
return
105+
}
106+
107+
$repoCred = [Management.Automation.PSCredential]::new($EmailAddress, (ConvertTo-SecureString -AsPlainText -Force $PersonalAccessToken))
108+
109+
Register-PSRepository -Name $targetName -SourceLocation $targetSource -PublishLocation $targetSource -InstallationPolicy Trusted -Credential $repoCred
110+
#endregion Create Credential and Register-PSRepository
111+
}
112+
}

0 commit comments

Comments
 (0)