-
Notifications
You must be signed in to change notification settings - Fork 82
/
020.PSResourceRepository.ps1
295 lines (238 loc) · 9.36 KB
/
020.PSResourceRepository.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<#
.SYNOPSIS
Determines if the repository is in the desired state.
.PARAMETER Ensure
If the repository should be present or absent on the server
being configured. Default values is 'Present'.
.PARAMETER Name
Specifies the name of the repository to manage.
.PARAMETER SourceLocation
Specifies the URI for discovering and installing modules from
this repository. A URI can be a NuGet server feed, HTTP, HTTPS,
FTP or file location.
.PARAMETER Credential
Specifies credentials of an account that has rights to register a repository.
.PARAMETER ScriptSourceLocation
Specifies the URI for the script source location.
.PARAMETER PublishLocation
Specifies the URI of the publish location. For example, for
NuGet-based repositories, the publish location is similar
to http://someNuGetUrl.com/api/v2/Packages.
.PARAMETER ScriptPublishLocation
Specifies the URI for the script publish location.
.PARAMETER Proxy
Specifies the URI of the proxy to connect to this PSResourceRepository.
.PARAMETER ProxyCredential
Specifies the Credential to connect to the PSResourceRepository proxy.
.PARAMETER InstallationPolicy
Specifies the installation policy. Valid values are 'Trusted'
or 'Untrusted'. The default value is 'Untrusted'.
.PARAMETER PackageManagementProvider
Specifies a OneGet package provider. Default value is 'NuGet'.
.PARAMETER Default
Specifies whether to set the default properties for the default PSGallery PSRepository.
Default may only be used in conjunction with a PSRepositoryResource named PSGallery.
The properties SourceLocation, ScriptSourceLocation, PublishLocation, ScriptPublishLocation, Credential,
and PackageManagementProvider may not be used in conjunction with Default.
.EXAMPLE
Invoke-DscResource -ModuleName ComputerManagementDsc -Name PSResourceRepository -Method Get -Property @{
Name = 'PSGallery'
SourceLocation = 'https://www.powershellgallery.com/api/v2'
ScriptSourceLocation = 'https://www.powershellgallery.com/api/v2/items/psscript'
PublishLocation = 'https://www.powershellgallery.com/api/v2/package/'
ScriptPublishLocation = 'https://www.powershellgallery.com/api/v2/package/'
InstallationPolicy = 'Untrusted'
PackageManagementProvider = 'NuGet'
}
This example shows how to call the resource using Invoke-DscResource.
#>
[DscResource()]
class PSResourceRepository : ResourceBase
{
[DscProperty()]
[Ensure]
$Ensure = [Ensure]::Present
[DscProperty(Key)]
[System.String]
$Name
[DscProperty()]
[System.String]
$SourceLocation
[DscProperty()]
[PSCredential]
$Credential
[DscProperty()]
[System.String]
$ScriptSourceLocation
[DscProperty()]
[System.String]
$PublishLocation
[DscProperty()]
[System.String]
$ScriptPublishLocation
[DscProperty()]
[System.String]
$Proxy
[DscProperty()]
[pscredential]
$ProxyCredential
[DscProperty()]
[InstallationPolicy]
$InstallationPolicy = [InstallationPolicy]::Untrusted
[DscProperty()]
[System.String]
$PackageManagementProvider
[DscProperty()]
[Nullable[System.Boolean]]
$Default
PSResourceRepository () : base ()
{
# These properties will not be enforced.
$this.ExcludeDscProperties = @(
'Name',
'Default'
)
}
[PSResourceRepository] Get()
{
return ([ResourceBase]$this).Get()
}
[void] Set()
{
([ResourceBase]$this).Set()
}
[Boolean] Test()
{
return ([ResourceBase] $this).Test()
}
hidden [void] Modify([System.Collections.Hashtable] $properties)
{
$params = @{
Name = $this.Name
}
if ($properties.ContainsKey('Ensure') -and $properties.Ensure -eq 'Absent' -and $this.Ensure -eq 'Absent')
{
# Ensure was not in desired state so the repository should be removed
Write-Verbose -Message ($this.localizedData.RemoveExistingRepository -f $this.Name)
Unregister-PSRepository @params
return
}
elseif ($properties.ContainsKey('Ensure') -and $properties.Ensure -eq 'Present' -and $this.Ensure -eq 'Present')
{
# Ensure was not in desired state so the repository should be created
$register = $true
}
else
{
# Repository exist but one or more properties are not in desired state
$register = $false
}
foreach ($key in $properties.Keys.Where({ $_ -ne 'Ensure' }))
{
Write-Verbose -Message ($this.localizedData.PropertyOutOfSync -f $key, $($this.$key))
$params[$key] = $properties.$key
}
if ( $register )
{
if ($this.Name -eq 'PSGallery')
{
Write-Verbose -Message ($this.localizedData.RegisterDefaultRepository -f $this.Name)
Register-PSRepository -Default
#* The user may have specified Proxy & Proxy Credential, or InstallationPolicy params
Set-PSRepository @params
}
else
{
if ([System.String]::IsNullOrEmpty($this.SourceLocation))
{
$errorMessage = $this.LocalizedData.SourceLocationRequiredForRegistration
New-InvalidArgumentException -ArgumentName 'SourceLocation' -Message $errorMessage
}
if ($params.Keys -notcontains 'SourceLocation')
{
$params['SourceLocation'] = $this.SourceLocation
}
Write-Verbose -Message ($this.localizedData.RegisterRepository -f $this.Name, $this.SourceLocation)
Register-PSRepository @params
}
}
else
{
Write-Verbose -Message ($this.localizedData.UpdateRepository -f $this.Name, $this.SourceLocation)
Set-PSRepository @params
}
}
hidden [System.Collections.Hashtable] GetCurrentState ([System.Collections.Hashtable] $properties)
{
$returnValue = @{
Ensure = [Ensure]::Absent
Name = $this.Name
SourceLocation = $this.SourceLocation
Default = $this.Default
}
Write-Verbose -Message ($this.localizedData.GetTargetResourceMessage -f $this.Name)
$repository = Get-PSRepository -Name $this.Name -ErrorAction SilentlyContinue
if ($repository)
{
$returnValue.Ensure = [Ensure]::Present
$returnValue.SourceLocation = $repository.SourceLocation
$returnValue.ScriptSourceLocation = $repository.ScriptSourceLocation
$returnValue.PublishLocation = $repository.PublishLocation
$returnValue.ScriptPublishLocation = $repository.ScriptPublishLocation
$returnValue.Proxy = $repository.Proxy
$returnValue.ProxyCredential = $repository.ProxyCredental
$returnValue.InstallationPolicy = [InstallationPolicy]::$($repository.InstallationPolicy)
$returnValue.PackageManagementProvider = $repository.PackageManagementProvider
}
else
{
Write-Verbose -Message ($this.localizedData.RepositoryNotFound -f $this.Name)
}
return $returnValue
}
<#
The parameter properties will contain the properties that was
assigned a value.
#>
hidden [void] AssertProperties([System.Collections.Hashtable] $properties)
{
Assert-Module -ModuleName PowerShellGet
Assert-Module -ModuleName PackageManagement
$assertBoundParameterParameters = @{
BoundParameterList = $this | Get-DscProperty -Type @('Key', 'Mandatory', 'Optional') -HasValue
MutuallyExclusiveList1 = @(
'Default'
)
MutuallyExclusiveList2 = @(
'SourceLocation'
'PackageSourceLocation'
'ScriptPublishLocation'
'ScriptSourceLocation'
'Credential'
'PackageManagementProvider'
)
}
Assert-BoundParameter @assertBoundParameterParameters
if ($this.Name -eq 'PSGallery')
{
if (-not $this.Default -and $this.Ensure -eq 'Present')
{
$errorMessage = $this.localizedData.NoDefaultSettingsPSGallery
New-InvalidArgumentException -ArgumentName 'Default' -Message $errorMessage
}
}
else
{
if ($this.Default)
{
$errorMessage = $this.localizedData.DefaultSettingsNoPSGallery
New-InvalidArgumentException -ArgumentName 'Default' -Message $errorMessage
}
}
if ($this.ProxyCredental -and (-not $this.Proxy))
{
$errorMessage = $this.localizedData.ProxyCredentialPassedWithoutProxyUri
New-InvalidArgumentException -ArgumentName 'ProxyCredential' -Message $errorMessage
}
}
}