-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPSGalleryModule.psm1
355 lines (313 loc) · 13.7 KB
/
PSGalleryModule.psm1
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
Class GalleryInfo {
$Title
$Id
$Type
$Version
$PackageDownloadURL
$NormalizedVersion
$Owners
$Authors
$ProjectUrl
$CompanyName
$PackageSize
$VersionDownloadCount
$ModuleDownloadCount
$Description
$Summary
$ReleaseNotes
$Created
$Published
$LastUpdated
$LastEdited
$IsPrerelease
$IsAbsoluteLatestVersion
$IsLatestVersion
$MinClientVersion
$Dependecies
$GalleryDetailsUrl
$Tags
$FileList
$ProcessorArchitecture
$CLRVersion
$PowerShellHostVersion
$DotNetFrameworkVersion
$PowerShellVersion
$GUID
$LicenseReportUrl
$LicenseNames
$LicenseUrl
$Copyright
$RequireLicenseAcceptance
$ReportAbuseUrl
$PackageHashAlgorithm
$PackageHash
$raw
GalleryInfo ($DataInput) {
$this.raw = $DataInput
$this.PackageDownloadURL = $DataInput.Content.Src
$this.Title = $DataInput.Title.'#Text'
$this.Id = $DataInput.Properties.id
$this.Version = $DataInput.Properties.Version
$this.Type = $DataInput.properties.ItemType
$this.VersionDownloadCount = $DataInput.Properties.VersionDownloadCount.'#Text'
$this.ModuleDownloadCount = $DataInput.Properties.DownloadCount.'#Text'
$this.PackageSize = $DataInput.properties.PackageSize.'#Text'
$this.NormalizedVersion = $DataInput.properties.NormalizedVersion
$this.Authors = $DataInput.Properties.Authors
$this.Copyright = $DataInput.Properties.Copyright
$this.Created = try { get-date $DataInput.properties.Created.'#Text' -ErrorAction stop }catch{$null}
$this.Description = $DataInput.properties.Description.'#Text'
$this.Tags = try { $DataInput.properties.tags.split(' ') } catch { $null }
$this.GalleryDetailsUrl = $DataInput.properties.GalleryDetailsUrl
$this.FileList = try { $DataInput.properties.FileList.Split('|') } catch { $null }
$this.Owners = $DataInput.properties.Owners
$this.CompanyName = $DataInput.properties.CompanyName
$this.ProcessorArchitecture = $DataInput.properties.ProcessorArchitecture.'#Text'
$this.CLRVersion = $DataInput.properties.CLRVersion.'#Text'
$this.PowerShellHostVersion = $DataInput.properties.PowerShellHostVersion.'#Text'
$this.DotNetFrameworkVersion = $DataInput.properties.DotNetFrameworkVersion.'#Text'
$this.PowerShellVersion = $DataInput.properties.PowerShellVersion
$this.GUID = $DataInput.properties.GUID
$this.LicenseReportUrl = $DataInput.properties.LicenseReportUrl.'#Text'
$this.LicenseNames = $DataInput.properties.LicenseReportUrl.'#Text'
$this.LicenseUrl = $DataInput.properties.LicenseUrl
$this.LastEdited = try { get-date $DataInput.properties.LastEdited.'#Text' -ErrorAction stop }catch{$null}
$this.MinClientVersion = $DataInput.properties.Summary.'#Text'
$this.Summary = $DataInput.properties.Summary.'#Text'
$this.RequireLicenseAcceptance = [Bool]$DataInput.properties.RequireLicenseAcceptance.'#Text'
$this.ReleaseNotes = $DataInput.properties.ReleaseNotes.'#Text'
$this.ReportAbuseUrl = $DataInput.properties.ReportAbuseUrl
$this.PackageHashAlgorithm = $DataInput.properties.PackageHashAlgorithm
$this.PackageHash = $DataInput.properties.PackageHash
$this.Published = try { get-date $DataInput.properties.Published.'#Text' -ErrorAction stop }catch{$null}
$this.LastUpdated = try { get-date $DataInput.properties.LastUpdated.'#Text' -ErrorAction stop }catch{$null}
$this.IsPrerelease = [bool]$DataInput.properties.IsPrerelease.'#Text'
$this.IsAbsoluteLatestVersion = [bool]$DataInput.properties.IsAbsoluteLatestVersion.'#Text'
$this.IsLatestVersion = [bool]$DataInput.properties.IsLatestVersion.'#Text'
$this.ProjectUrl = If ( [bool]$DataInput.properties.ProjectUrl.null ) {$null}else{$DataInput.properties.ProjectUrl}
}
[Object] Download () {
$PackageName = $this.ID + '.' + $this.NormalizedVersion + '.zip'
$OutFile = Join-Path -Path $PWD -ChildPath $PackageName
Invoke-WebRequest -Uri $this.PackageDownloadURL -OutFile $OutFile -ErrorAction Stop
return (Get-Item -Path $OutFile)
}
[Object] Download ($Path) {
If ( ! ( Test-Path $Path) ) {
Throw [System.IO.DirectoryNotFoundException]::new("Path Not Found: $Path")
}
$PackageName = $this.ID + '.' + $this.NormalizedVersion + '.zip'
$OutFile = Join-Path -Path $Path -ChildPath $PackageName
Invoke-WebRequest -Uri $this.PackageDownloadURL -OutFile $OutFile -ErrorAction Stop
return (Get-Item -Path $OutFile)
}
}
function Find-GalleryModule {
<#
.SYNOPSIS
Simple Function to retrieve Module(s) info(s) from the PSGallery
.DESCRIPTION
Simple Function to retrieve Module(s) info(s) from the PSGallery, and download the module as a zip file.
.EXAMPLE
PS C:\> Find-GalleryModule -Module PSClassutils -latestversion
Will retrieve infos, about all the modules named psclassutils.
Id : PSClassUtils
Version : 2.6.3
NormalizedVersion : 2.6.3
Authors : Stéphane van Gulick
Copyright : (c) 2018 TAAVAST3. All rights reserved.
Created : Created
Dependencies :
Description : Contains a set of utilities to work with Powershell Classes
...
DownloadCount : 240
VersionDownloadCount : 1381
.EXAMPLE
PS C:\> Find-GalleryModule -Author Stéphane -latestversion
Will retrieve infos, about all the modules created by authors starting with "Stéphane".
Id : PSClassUtils
Version : 2.6.3
NormalizedVersion : 2.6.3
Authors : Stéphane van Gulick
Copyright : (c) 2018 TAAVAST3. All rights reserved.
Created : Created
Dependencies :
Description : Contains a set of utilities to work with Powershell Classes
...
.EXAMPLE
PS C:\> Find-GalleryModule -Module PSClassutils -latestversion -Download
Search for module PSClassUtils and download the package as a zip file in the current directory.
Répertoire : C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 04/09/2019 21:51 1854235 PSClassUtils.2.6.3.zip
.EXAMPLE
PS C:\> Find-GalleryModule -Date 16/09/2019 -latestversion | select -Property Authors,Title,Version,Published
will find all published module this day.
Authors Title Version Published
------- ----- ------- ---------
Przemyslaw Klys ADEssentials 0.0.18 16/09/2019 21:12:10
a.krick@outlook.com AKPT 5.11.4.0 16/09/2019 07:42:50
R. Josh Nylander AMAG-SMSPowershell 1.1.4 16/09/2019 00:38:25
Esri ArcGIS 2.1.1 16/09/2019 18:45:28
AzSK Team AzSK 4.1.0 16/09/2019 14:07:54
AzSK Team AzSKPreview 4.1.2 16/09/2019 11:29:40
David Stein CMHealthcheck 1.0.10 16/09/2019 23:26:01
...
.EXAMPLE
PS C:\> Find-GalleryModule -Module p* -LatestVersion -PSEditionType Desktop | select id,version
Find Modules with id starting with "p", latestversion and are compatible with Powershell Desktop.
Id Version
-- -------
PackageManagement 1.4.4
PartnerCenter 2.0.1909.2
PartnerCenter.NetCore 1.5.1908.1
passwordstate-management 4.0.5
Pester 4.9.0
.INPUTS
Module(s) Names, or partial Module name
You can use the * wildcard if you dont know the exact name of the module
Author Name.
Date, find all module published at a certain date.
.OUTPUTS
Custom [GalleryInfo] Type, representing a Module Infos from the PSGallery
.NOTES
Go check: /~https://github.com/LxLeChat/PSGalleryModule
#>
[CmdletBinding()]
Param (
[Parameter(ValueFromPipelineByPropertyName=$True,ValueFromPipeline=$True,ParameterSetName='Module')]
[Alias("Name")]
[String[]]$Module,
[Parameter(ParameterSetName='Author')]
[String]$Author,
[ValidateSet("LatestVersion","PreRelease")]
[String]$Version,
[ValidateSet("Core","Desktop")]
[String]$PSEditionType,
[Parameter(ParameterSetName='Date')]
[ValidateScript({get-date $_})]
[String]$Date,
[Switch]$Download,
[String]$OutPath
)
Begin {
$bQ = '$filter='
$Q = ''
$i = 0
}
Process {
Switch ( $PSCmdlet.ParameterSetName ) {
'Author' {
## Build Query, api calls are made in the end block
$Q = "startswith(Authors,'$Author')"
Switch ($Version) {
## Will look for LatestVersion Only
'LatestVersion' {
$Q = '(' + $Q + ') and IsLatestVersion'
}
## Will look for Latest PreRelease version Only
'PreRelease' {
$Q = '(' + $Q + ') and IsPrerelease and IsAbsoluteLatestVersion'
}
}
If ( $PSEditionType ) {
Switch ( $PSEditionType ) {
"Core" { $Q = $Q + " and indexof(Tags,'PSEdition_Core') ge 0"}
"Desktop" { $Q = $Q + " and indexof(Tags,'PSEdition_Desktop') ge 0"}
}
}
}
'Module' {
## Build Query, api calls are made in the end block
Foreach ( $M in $Module ) {
If ( $i -gt 0 ) {
$Q = $Q + ' or '
}
switch -Regex ($M) {
"^\*.+\*$" {$tQ = "indexof(Id,'$($M.replace('*',''))') ge 0";break}
"^\*.+" {$tQ = "endswith(Id,'$($M.trimstart('*'))')";break}
".+\*$" {$tQ = "startswith(Id,'$($M.trimend('*'))')";break}
"^\*$" {$tQ = "startswith(Id,'')";break}
default {$tQ = "Id eq '$M'"}
}
$Q = $Q + $tQ
$i++
}
Switch ($Version) {
## Will look for LatestVersion Only
'LatestVersion' {
$Q = '(' + $Q + ') and IsLatestVersion'
}
## Will look for Latest PreRelease version Only
'PreRelease' {
$Q = '(' + $Q + ') and IsPrerelease and IsAbsoluteLatestVersion'
}
}
## Will look for specific Tags
If ( $PSEditionType ) {
Switch ( $PSEditionType ) {
"Core" { $Q = $Q + " and indexof(Tags,'PSEdition_Core') ge 0"}
"Desktop" { $Q = $Q + " and indexof(Tags,'PSEdition_Desktop') ge 0"}
}
}
}
'Date' {
$StartDate = get-date -date $date -Hour 0 -Minute 0 -Second 0 -Format s
$date1 = (get-date -Date $date).AddDays(1)
$EndDate = get-date -date $date1 -Hour 0 -Minute 0 -Second 0 -Format s
$Q = "Published gt DateTime'$StartDate' and Published lt DateTime'$EndDate'"
Switch ($Version) {
## Will look for LatestVersion Only
'LatestVersion' {
$Q = '(' + $Q + ') and IsLatestVersion'
}
## Will look for Latest PreRelease version Only
'PreRelease' {
$Q = '(' + $Q + ') and IsPrerelease and IsAbsoluteLatestVersion'
}
}
## Will look for specific Tags
If ( $PSEditionType ) {
Switch ( $PSEditionType ) {
"Core" { $Q = $Q + " and indexof(Tags,'PSEdition_Core') ge 0"}
"Desktop" { $Q = $Q + " and indexof(Tags,'PSEdition_Desktop') ge 0"}
}
}
}
}
}
End {
$fQ = $bQ + $Q
$Uri = "https://www.powershellgallery.com/api/v2/Packages()?$fQ&`$orderby=Id"
#$Uri
#break;
$skip = 0
$BaseUri = $uri
$y = 100
While ( $y -eq 100 ) {
$y = 0
## Build new page
If ( $skip -gt 0 ) {
$uri = $BaseUri + "&`$skip=$skip"
}
## ApiCall
([Array](Invoke-RestMethod -Method GET -Uri $Uri)).foreach({
If ( $Download ) {
If ( $OutPath ) {
[GalleryInfo]::new($_).Download($OutPath)
} Else {
[GalleryInfo]::new($_).Download()
}
} Else {
[GalleryInfo]::new($_)
}
$y++
})
## Pagination
If ( $y -eq 100 ) {
$skip = $skip + $y
}
}
}
}