@@ -29,14 +29,19 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
2929 #region Members
3030
3131 private readonly string PSGalleryRepoName = "PSGallery" ;
32+ private readonly string MicrosoftArtifactRegistryRepoName = "MicrosoftArtifactRegistry" ;
33+ private readonly string MicrosoftArtifactRegistryRepoUri = "https://mcr.microsoft.com" ;
3234 private readonly string PSGalleryRepoUri = "https://www.powershellgallery.com/api/v2" ;
3335 private const int DefaultPriority = 50 ;
3436 private const bool DefaultTrusted = false ;
37+ private const bool MARDefaultTrusted = true ;
38+ private const int MARDefaultPriority = 40 ;
3539 private const string NameParameterSet = "NameParameterSet" ;
3640 private const string PSGalleryParameterSet = "PSGalleryParameterSet" ;
3741 private const string RepositoriesParameterSet = "RepositoriesParameterSet" ;
3842 private Uri _uri ;
3943 private CredentialProviderDynamicParameters _credentialProvider ;
44+ private const string MARParameterSet = "MARParameterSet" ;
4045
4146 #endregion
4247
@@ -62,6 +67,13 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
6267 [ Parameter ( Mandatory = true , ParameterSetName = PSGalleryParameterSet , HelpMessage = "PSGallery switch used to indicate registering PSGallery repository." ) ]
6368 public SwitchParameter PSGallery { get ; set ; }
6469
70+ /// <summary>
71+ /// When specified, registers Microsoft Artifact Registry repository.
72+ /// </summary>
73+ [ Parameter ( Mandatory = true , ParameterSetName = MARParameterSet , HelpMessage = "Switch used to indicate registering Microsoft Artifact Registry repository." ) ]
74+ [ Alias ( "MAR" ) ]
75+ public SwitchParameter MicrosoftArtifactRegistry { get ; set ; }
76+
6577 /// <summary>
6678 /// Specifies a hashtable of repositories and is used to register multiple repositories at once.
6779 /// </summary>
@@ -74,6 +86,7 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
7486 /// </summary>
7587 [ Parameter ( ParameterSetName = NameParameterSet ) ]
7688 [ Parameter ( ParameterSetName = PSGalleryParameterSet ) ]
89+ [ Parameter ( ParameterSetName = MARParameterSet ) ]
7790 public SwitchParameter Trusted { get ; set ; }
7891
7992 /// <summary>
@@ -84,8 +97,9 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
8497 /// </summary>
8598 [ Parameter ( ParameterSetName = NameParameterSet ) ]
8699 [ Parameter ( ParameterSetName = PSGalleryParameterSet ) ]
100+ [ Parameter ( ParameterSetName = MARParameterSet ) ]
87101 [ ValidateRange ( 0 , 100 ) ]
88- public int Priority { get ; set ; } = DefaultPriority ;
102+ public int Priority { get ; set ; }
89103
90104 /// <summary>
91105 /// Specifies the Api version of the repository to be set.
@@ -122,6 +136,7 @@ public object GetDynamicParameters()
122136 // It should also not appear when using the 'Repositories' parameter set.
123137 if ( ParameterSetName . Equals ( PSGalleryParameterSet ) ||
124138 ParameterSetName . Equals ( RepositoriesParameterSet ) ||
139+ ParameterSetName . Equals ( MARParameterSet ) ||
125140 PSRepositoryInfo . IsValidContainerRegistryURL ( Uri ) )
126141 {
127142 return null ;
@@ -149,6 +164,18 @@ protected override void ProcessRecord()
149164 repoApiVersion = ApiVersion ;
150165 }
151166
167+ if ( ! MyInvocation . BoundParameters . ContainsKey ( nameof ( Priority ) ) )
168+ {
169+ if ( ParameterSetName . Equals ( MARParameterSet , StringComparison . OrdinalIgnoreCase ) )
170+ {
171+ Priority = MARDefaultPriority ;
172+ }
173+ else
174+ {
175+ Priority = DefaultPriority ;
176+ }
177+ }
178+
152179 PSRepositoryInfo . CredentialProviderType ? credentialProvider = _credentialProvider ? . CredentialProvider ;
153180
154181 switch ( ParameterSetName )
@@ -218,6 +245,25 @@ protected override void ProcessRecord()
218245 }
219246 break ;
220247
248+ case MARParameterSet :
249+ if ( MicrosoftArtifactRegistry )
250+ {
251+ try
252+ {
253+ bool trustedValue = Trusted . IsPresent ? Trusted : MARDefaultTrusted ;
254+ items . Add ( MicrosoftArtifactRegistryParameterSetHelper ( Priority , trustedValue ) ) ;
255+ }
256+ catch ( Exception e )
257+ {
258+ ThrowTerminatingError ( new ErrorRecord (
259+ new PSInvalidOperationException ( e . Message ) ,
260+ "ErrorInMARParameterSet" ,
261+ ErrorCategory . InvalidArgument ,
262+ this ) ) ;
263+ }
264+ }
265+ break ;
266+
221267 default :
222268 Dbg . Assert ( false , "Invalid parameter set" ) ;
223269 break ;
@@ -262,6 +308,34 @@ private PSRepositoryInfo PSGalleryParameterSetHelper(int repoPriority, bool repo
262308 return addedRepo ;
263309 }
264310
311+ private PSRepositoryInfo MicrosoftArtifactRegistryParameterSetHelper ( int repoPriority , bool repoTrusted )
312+ {
313+ WriteDebug ( "In RegisterPSResourceRepository::MicrosoftArtifactRegistryParameterSetHelper()" ) ;
314+ Uri marUri = new Uri ( MicrosoftArtifactRegistryRepoUri ) ;
315+ WriteDebug ( "Internal name and uri values for Microsoft Artifact Registry are hardcoded and validated. Priority and trusted values, if passed in, also validated" ) ;
316+ var addedRepo = RepositorySettings . AddToRepositoryStore ( MicrosoftArtifactRegistryRepoName ,
317+ marUri ,
318+ repoPriority ,
319+ repoTrusted ,
320+ apiVersion : null ,
321+ repoCredentialInfo : null ,
322+ credentialProvider : null ,
323+ Force ,
324+ this ,
325+ out string errorMsg ) ;
326+
327+ if ( ! string . IsNullOrEmpty ( errorMsg ) )
328+ {
329+ ThrowTerminatingError ( new ErrorRecord (
330+ new PSInvalidOperationException ( errorMsg ) ,
331+ "RepositoryCredentialSecretManagementUnavailableModule" ,
332+ ErrorCategory . ResourceUnavailable ,
333+ this ) ) ;
334+ }
335+
336+ return addedRepo ;
337+ }
338+
265339 private List < PSRepositoryInfo > RepositoriesParameterSetHelper ( )
266340 {
267341 WriteDebug ( "In RegisterPSResourceRepository::RepositoriesParameterSetHelper()" ) ;
@@ -323,7 +397,7 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
323397 return null ;
324398 }
325399
326- if ( repo [ "Name" ] . ToString ( ) . Equals ( "PSGallery" ) )
400+ if ( repo [ "Name" ] . ToString ( ) . Equals ( "PSGallery" , StringComparison . OrdinalIgnoreCase ) )
327401 {
328402 WriteError ( new ErrorRecord (
329403 new PSInvalidOperationException ( "Cannot register PSGallery with -Name parameter. Try: Register-PSResourceRepository -PSGallery" ) ,
@@ -334,6 +408,17 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
334408 return null ;
335409 }
336410
411+ if ( repo [ "Name" ] . ToString ( ) . Equals ( "MAR" , StringComparison . OrdinalIgnoreCase ) )
412+ {
413+ WriteError ( new ErrorRecord (
414+ new PSInvalidOperationException ( "Cannot register MAR with -Name parameter. The MAR repository is automatically registered. Try: Reset-PSResourceRepository to restore default repositories." ) ,
415+ "MARProvidedAsNameRepoPSet" ,
416+ ErrorCategory . InvalidArgument ,
417+ this ) ) ;
418+
419+ return null ;
420+ }
421+
337422 if ( ! repo . ContainsKey ( "Uri" ) || repo [ "Uri" ] == null || String . IsNullOrEmpty ( repo [ "Uri" ] . ToString ( ) ) )
338423 {
339424 WriteError ( new ErrorRecord (
0 commit comments