Public/Register-OriAzBopRepository.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<#
.DESCRIPTION
This function Register repository
 
.SYNOPSIS
This function Register repository
     
.PARAMETER DevOpsAccount
The name of the dev ops account. Default value is 'oriflame'
       
.PARAMETER RepositoryCredential
Credentials for connection to repository
 
.PARAMETER PsProjectName
String array of project name in Dev Ops for connect via PowershellGet for getting Powershell modules e.g. PackageManagementFeed
 
.PARAMETER NugetProjectName
String array of project name in Dev Ops for connect via Nuget for getting Powershell modules e.g. Online, GlobalDev, DeploymentPackages
 
.PARAMETER SkipPrompt
When is set it does not prompt
 
.PARAMETER RegisterViaPSRepository
When is set register via Register-PSRepository, otherwise use Register-PackageSource.
 
 
.EXAMPLE
$password = ConvertTo-SecureString 'password1' -AsPlainText -Force
$RepositoryCredential = New-Object System.Management.Automation.PSCredential 'Wuhan@myemail.com',$password
 
$password = ConvertTo-SecureString 'password2' -AsPlainText -Force
$RemoteCredential = New-Object System.Management.Automation.PSCredential 'MYDOMAIN\login',$password
 
Register-OriAzBopRepository `
-DevOpsAccount 'oriflame' `
-RepositoryCredential $RepositoryCredential `
-PsProjectName @('PackageManagementFeed') `
-NugetProjectName @('DeploymentPackages')
#>


function Register-OriAzBopRepository {
    
  [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "Low")]
  param
  (   
      
    [parameter(Mandatory = $false, HelpMessage = 'The name of the dev ops account')]
    [string] $DevOpsAccount = $Script:VstsAccount,
      
    [parameter(Mandatory = $true, HelpMessage = 'Credentials for connection to repository')]
    [PSCredential] $RepositoryCredential,

    [parameter(Mandatory = $true, HelpMessage = 'String array of project name in Dev Ops for connect via PowershellGet for getting Powershell modules e.g. PackageManagementFeed')]
    [string[]] $PsProjectName,

    [parameter(Mandatory = $true, HelpMessage = 'String array of project name in Dev Ops for connect via Nuget for getting Powershell modules e.g. Online, GlobalDev, DeploymentPackages')]
    [string[]] $NugetProjectName,

    [parameter(Mandatory = $false, HelpMessage = 'When is set it does not prompt')]
    [Switch] $SkipPrompt,

    [Parameter(Mandatory = $false, HelpMessage = "When is set register via Register-PSRepository, otherwise use Register-PackageSource.")]
    [switch] $RegisterViaPSRepository

  )
  
  #in case of any error we want to stop execution, in stderr having the error. Use try-catch to handle errors if needed.
  $ErrorActionPreference = "Stop";
  Write-Verbose "-- Register-OriAzBopRepository --"
  $MachineName = "$($env:COMPUTERNAME)"
  Write-Verbose "MachineName: $MachineName"
  Write-Verbose "RemoteCredential: $(ConvertTo-Json $RemoteCredential)"
  Write-Verbose "DevOpsAccount: $DevOpsAccount"
  Write-Verbose "PsProjectName: $(ConvertTo-Json $PsProjectName)"
  Write-Verbose "NugetProjectName: $(ConvertTo-Json $NugetProjectName)"
      
  # Due some back compatibility is the same location already registered.
  # To enable same name in name should be the previous registration removed if needed
  Unregister-PSRepository -Name Oriflame.Ps.Gallery -ErrorAction SilentlyContinue

  $registeredPsFeeds = Get-PSRepository
  foreach ($feed in $PsProjectName) {

    
    $uri = 'https://{0}.pkgs.visualstudio.com/_packaging/{1}/nuget/v2' -f $DevOpsAccount, $feed
    # Note: alternative new URI
    # $uri = 'https://pkgs.dev.azure.com/{0}/_packaging/{1}/nuget/v2' -f $DevOpsAccount, $feed
  
    $RepoAlreadyExists = $false
    foreach ($OneExistsingFeed in $registeredPsFeeds) {
      if ($OneExistsingFeed.SourceLocation -ilike $uri) {
        Write-Verbose "Already registered repo [$($OneExistsingFeed.Name), $($OneExistsingFeed.SourceLocation)] NuGet feed based on URI compare."
        $RepoAlreadyExists = $true
        continue
      }
      if ($OneExistsingFeed.Name -ilike $feed) {
        Write-Verbose "Already registered repo [$($OneExistsingFeed.Name), $($OneExistsingFeed.SourceLocation)] NuGet feed based on repo name compare."
        $RepoAlreadyExists = $true
        continue
      }
    }
    if ($RepoAlreadyExists) {
      Write-Verbose "Skip to another required PsProjectName"
      continue
    }
    else {
      Write-Verbose "Register [$feed] PowershellGet feed."
    }
  
    if ($RegisterViaPSRepository.IsPresent) {
      # Parameters for using the Register-PSRepository
      [Hashtable] $RegisterParam = @{
        Name               = $feed
        SourceLocation     = $uri
        PublishLocation    = $uri
        InstallationPolicy = 'Trusted'
        Credential         = $RepositoryCredential
      }
    }
    else {
      # While using the Register-PackageSource for repo register are parameters little bit different.
      [Hashtable] $RegisterParam = @{
        ProviderName          = 'PowerShellGet'
        Name                  = $feed
        Location              = $uri
        Credential            = $RepositoryCredential
        Trusted               = $true
        PublishLocation       = $uri
        ScriptPublishLocation = $uri
      }
    }
  
    Write-Verbose "Register-PSRepository params:"
    Write-Verbose (ConvertTo-Json  $RegisterParam)

    if ($SkipPrompt.IsPresent -or $PSCmdlet.ShouldProcess("Register Powsershell reposiory(PS)", $MachineName)) {
      Register-OriAzBopInternalPSRepository `
        -RegisterRepositoryParams $RegisterParam `
        -RegisterViaPSRepository:$RegisterViaPSRepository `
        -Verbose:$VerbosePreference `
        -Debug:$DebugPreference 
    }
            
  }

  Write-Verbose 'Begin register nuget part'
  $registeredNugetFeeds = Get-OriAzBopInternalPackageSource

  foreach ($feed in $NugetProjectName) {
    Write-Verbose "try nuget feed [$feed]"
    $uri = 'https://pkgs.dev.azure.com/{0}/_packaging/{1}/nuget/v2' -f $DevOpsAccount, $feed
    #Note: alternate old URI
    # $uri = 'https://{0}.pkgs.visualstudio.com/_packaging/{1}/nuget/v2' -f $DevOpsAccount, $feed
  
    $RepoAlreadyExists = $false
    foreach ($OneExistsingFeed in $registeredNugetFeeds) {
      Write-Verbose "Compare URI [$($OneExistsingFeed.Name), $($OneExistsingFeed.Location)] and [$uri]"
      if ($OneExistsingFeed.Location -ilike $uri) {
        Write-Verbose "Already registered repo [$feed] NuGet feed based on URI compare."
        $RepoAlreadyExists = $true
        continue
      }
      Write-Verbose "Compare name [$($OneExistsingFeed.Name), $($OneExistsingFeed.Location)] and [$feed]"
      if ($OneExistsingFeed.Name -ilike $feed) {
        Write-Verbose "Already registered repo [$feed] NuGet feed based on repo name compare."
        $RepoAlreadyExists = $true
        continue
      }
    }

    Write-Verbose "End of compare [$feed]"
    if ($RepoAlreadyExists) {
      Write-Verbose "Skip to another required NugetProjectName"
      continue
    }
    else {
      Write-Verbose "Register [$feed] NuGet feed."
    }
  
            
    #Note: alternative old URI "https://oriflame.pkgs.visualstudio.com/_packaging/$feed/nuget/v2"
  
    $RegisterParam = @{
      Name         = $feed
      Location     = $uri
      Trusted      = $true
      Credential   = $RepositoryCredential
      ProviderName = 'NuGet'
    }
            
  
    Write-Verbose "Register-PackageSource params:"
    Write-Verbose (ConvertTo-Json  $RegisterParam)
    # @RegisterParam
    #Register-PackageSource `
    # @RegisterParam `
    # -Verbose:$VerbosePreference `
    # -Debug:$DebugPreference
    if ($SkipPrompt -or $PSCmdlet.ShouldProcess("Register Package Source reposiory(GIT) $feed", $MachineName)) {
      Register-OriAzBopInternalPackageSource `
        -RegisterPackageSourceParam $RegisterParam `
        -Verbose:$VerbosePreference `
        -Debug:$DebugPreference 
    }

  }
  Write-Verbose "Result of registred repo:"
  if ($VerbosePreference) {
    Get-OriAzBopInternalPackageSource `
      -Verbose:$VerbosePreference `
      -Debug:$DebugPreference | ForEach-Object -Process { $_ | Out-String | Write-Verbose }
  }
  
  Write-Verbose "-- End of Register-OriAzBopRepository --"
}