This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMyAlbum.psm1
823 lines (665 loc) · 27.6 KB
/
MyAlbum.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
# This is a sample PackageManagement provider. It is trying to discover photos in your remote file repository
# and installs them to your local folder.
# Import the localized Data
Microsoft.PowerShell.Utility\Import-LocalizedData LocalizedData -filename MyAlbum.Resource.psd1
#region Local variable definitions
# Define the provider name
$script:ProviderName = "MyAlbum"
# The folder where stores the provider configuration file
$script:LocalPath="$env:LOCALAPPDATA\Contoso\$script:ProviderName"
$script:RegisteredPackageSources = $null
$script:RegisteredPackageSourcesFilePath = Microsoft.PowerShell.Management\Join-Path -Path $script:LocalPath -ChildPath "MyAlbumPackageSource.xml"
# Wildcard pattern matching configuration
$script:wildcardOptions = [System.Management.Automation.WildcardOptions]::CultureInvariant -bor `
[System.Management.Automation.WildcardOptions]::IgnoreCase
#endregion
#region Provider APIs Implementation
# Mandatory function for the PackageManagement providers. It returns the name of your provider.
function Get-PackageProviderName {
return $script:ProviderName
}
# Mandatory function for the PackageManagement providers. It initializes your provider before performing any actions.
function Initialize-Provider {
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Initialize-Provider'))
#add your intialize code here
}
<# Optional function that indicates what featues your provider supports. Returns a collection of features this provider supports.
This is primarily for others to leveage your provider. Here is the existing features defined by PackageManagement:
SupportsPowerShellModules = "supports-powershell-modules"; === find, install, powershell modules
SupportsRegexSearch = "supports-regex-search"; === support Regualar expression search
SupportsWildcardSearch = "supports-wildcard-search"; === support wildcard search
SupportedExtensions = "file-extensions"; === package file extensions, e.g., .msi, .nupkg
SupportedSchemes = "uri-schemes"; === url shemes, e.g., http, https, file
MagicSignatures = "magic-signatures"; === bytes at the begining of a package file, .cab, .zip
#>
function Get-Feature
{
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Get-Feature'))
# Write out to the host. In this case, PackageManagement is the host.
Write-Output -InputObject (New-Feature -name "file-extensions" -values @(".png"))
<#
Add more features that your provider supports here. e.g.,
Write-Output -InputObject (New-Feature -name "supports-python-modules")
#>
}
# Optional function that returns dynamic parameters defined by the provider to the PackageManagement.
function Get-DynamicOptions
{
param
(
[Microsoft.PackageManagement.MetaProvider.PowerShell.OptionCategory]
$category
)
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Get-DynamicOptions'))
# There are available categories defined by PackageManagement:
# Package - for searching for packages
# Source - for package sources
# Install - for Install/Uninstall/Get-InstalledPackage
switch($category)
{
Package {
Write-Output -InputObject (New-DynamicOption -Category $category -Name "Filter" -ExpectedType String -IsRequired $false)
}
Install
{
Write-Output -InputObject (New-DynamicOption -Category $category -Name "Destination" -ExpectedType String -IsRequired $true)
}
}
}
# Optional function that gets called when the user is registering a package source.
# .e.g, Register-PackageSource -Name demo -Location C:\CameraRoll -ProviderName MyAlbum
# If your provider supports Register-PackageSource, it is required to implement this function.
function Add-PackageSource
{
[CmdletBinding()]
param
(
[string]
$Name,
[string]
$Location,
[bool]
$Trusted
)
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Add-PackageSource'))
Get-PSDrive
if(-not (Microsoft.PowerShell.Management\Test-Path -path $Location))
{
ThrowError -ExceptionName "System.ArgumentException" `
-ExceptionMessage ($LocalizedData.PathNotFound -f ($Location)) `
-ErrorId "PathNotFound" `
-CallerPSCmdlet $PSCmdlet `
-ErrorCategory InvalidArgument `
-ExceptionObject $Location
return
}
# We do not allow "Register-PackageSource -Name a*"
if(Test-WildcardPattern $Name)
{
ThrowError -ExceptionName "System.ArgumentException" `
-ExceptionMessage ($LocalizedData.PackageSourceNameContainsWildCards -f ($Name)) `
-ErrorId "PackageSourceNameContainsWildCards" `
-CallerPSCmdlet $PSCmdlet `
-ErrorCategory InvalidArgument `
-ExceptionObject $Name
return
}
Set-PackageSourcesVariable -Force
# Add new package source
$packageSource = Microsoft.PowerShell.Utility\New-Object PSCustomObject -Property ([ordered]@{
Name = $Name
SourceLocation = $Location.TrimEnd("\")
Trusted=$Trusted
Registered= $true
InstallationPolicy = if($Trusted) {'Trusted'} else {'Untrusted'}
})
$script:RegisteredPackageSources.Add($Name, $packageSource)
Write-Verbose -Message ($LocalizedData.SourceRegistered -f ($Name, $Location))
# Persist the package sources
Save-PackageSources
# yield the package source to OneGet
Write-Output -InputObject (New-PackageSourceAndYield -Source $packageSource)
}
# Optional function that unregisters a package Source. e.g., Unregister-PackageSource -Name album.
# It is required to implement this function for providers that support Unregister-PackageSource.
function Remove-PackageSource
{
param
(
[string]
$Name
)
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Remove-PackageSource'))
Set-PackageSourcesVariable -Force
# Check if $Name contains any wildcards
if(Test-WildcardPattern $Name)
{
$message = $LocalizedData.PackageSourceNameContainsWildCards -f ($Name)
Write-Error -Message $message -ErrorId "PackageSourceNameContainsWildCards" -Category InvalidOperation -TargetObject $Name
return
}
# Error out if the specified source name is not in the registered package sources.
if(-not $script:RegisteredPackageSources.Contains($Name))
{
$message = $LocalizedData.PackageSourceNotFound -f ($Name)
Write-Error -Message $message -ErrorId "PackageSourceNotFound" -Category InvalidOperation -TargetObject $Name
return
}
# Remove the SourcesToBeRemoved
$script:RegisteredPackageSources.Remove($Name)
# Persist the package sources
Save-PackageSources
Write-Verbose ($LocalizedData.PackageSourceUnregistered -f ($Name))
}
# This is an optional function that returns the registered package sources or the sources the provider can handle.
# For exmaple, it gets called during find-package, install-package, get-packagesource etc.
# PackageManagement uses this method to identify which provider can handle the packages from a particular source location.
# Therefore in general this function needs to be implemented.
function Resolve-PackageSource
{
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Resolve-PackageSource'))
# Use the $request object to get user's cmdline parameter values, or return the values back to PackageManagement.
# Get the value of "-Source" from user's commandline input
$SourceName = $request.PackageSources
# get Sources from the registered config file
Set-PackageSourcesVariable
if(-not $SourceName)
{
$SourceName = "*"
}
foreach($src in $SourceName)
{
if($request.IsCanceled) { return }
# Get the sources that registered before
$wildcardPattern = New-Object System.Management.Automation.WildcardPattern $src,$script:wildcardOptions
$sourceFound = $false
$script:RegisteredPackageSources.GetEnumerator() |
Microsoft.PowerShell.Core\Where-Object {$wildcardPattern.IsMatch($_.Key)} |
Microsoft.PowerShell.Core\ForEach-Object {
$source = $script:RegisteredPackageSources[$_.Key]
$packageSource = New-PackageSourceAndYield -Source $source
Write-Output -InputObject $packageSource
$sourceFound = $true
}
# If a user does specify -Source but not registered
if(-not $sourceFound)
{
# Get source name from the source location in case a user passes in location instead of name
$sourceName = Get-SourceName -Location $src
if($sourceName)
{
$source = $script:RegisteredPackageSources[$sourceName]
$packageSource = New-PackageSourceAndYield -Source $source
Write-Output -InputObject $packageSource
}
# So far we found the given source is not a registered package source Name nor Location
# It depends on your provider's implementation whether you want to support unregistered source.
# If you do, add your code here. In this example, we only suppport the registered ones.
elseif( -not (Test-WildcardPattern $src))
{
$message = $LocalizedData.PackageSourceNotFound -f ($src)
Write-Error -Message $message -ErrorId "PackageSourceNotFound" -Category InvalidOperation -TargetObject $src
}
}
}
}
# Optional function that finds packages by given name and version information.
# It is required to implement this function for the providers that support find-package. For example, find-package -ProviderName MyAlbum -Source demo.
function Find-Package {
param(
[string] $name,
[string] $requiredVersion,
[string] $minimumVersion,
[string] $maximumVersion
)
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Find-Package'))
# Read in the registered package source information to the memory
Set-PackageSourcesVariable
$ValidationResult = Validate-VersionParameters -Name $Name `
-MinimumVersion $MinimumVersion `
-MaximumVersion $MaximumVersion `
-RequiredVersion $RequiredVersion
if(-not $ValidationResult)
{
# Return now as the version validation failed already
return
}
# Get the cmdlet parameter values that were passed from the user via the $request.Options.
# Here we can find out the package source name passed by a user.
<#
Commonly used properties of $request object:
PackageSources -- -Source
Options -- Get any user's input via Options
Credential -- -Credential
IsCanceled -- Is operation cancelling?
#>
$options = $request.Options
foreach( $o in $options.Keys )
{
Write-Debug ( "OPTION: {0} => {1}" -f ($o, $options[$o]) )
}
# Check if a user specifies -Source
$selectedSources = @()
if($options -and $options.ContainsKey('Source'))
{
# Finding the matched package sources from the registered ones
$sourceNames = $($options['Source'])
Write-Verbose ($LocalizedData.SpecifiedSourceName -f ($sourceNames))
foreach($sourceName in $sourceNames)
{
if($script:RegisteredPackageSources.Contains($sourceName))
{
# Found the matched registered source
$selectedSources += $script:RegisteredPackageSources[$sourceName]
}
else
{
$sourceByLocation = Get-SourceName -Location $sourceName
if ($sourceByLocation -ne $null)
{
$selectedSources += $script:RegisteredPackageSources[$sourceByLocation]
}
else
{
$message = $LocalizedData.PackageSourceNotFound -f ($sourceName)
ThrowError -ExceptionName "System.ArgumentException" `
-ExceptionMessage $message `
-ErrorId "PackageSourceNotFound" `
-CallerPSCmdlet $PSCmdlet `
-ErrorCategory InvalidArgument `
-ExceptionObject $sourceName
}
}
}
}
else
{
# User does not specify -Source, we will use the registered sources
Write-Verbose $LocalizedData.NoSourceNameIsSpecified
$script:RegisteredPackageSources.Values | Microsoft.PowerShell.Core\ForEach-Object { $selectedSources += $_ }
}
# finding the package
foreach($source in $selectedSources)
{
if($request.IsCanceled) { return }
$location = $source.SourceLocation
if(-not (Test-Path $location)) { continue }
# Find the photos
$files = Get-ChildItem -Path $location -Filter '*.png' -Recurse | `
Where-Object { ($_.PSIsContainer -eq $false) -and ( $_.Name -like "*$name*") }
foreach($file in $files)
{
<#add code here for handling filter #>
if($request.IsCanceled) { return }
# Note: FastPackageReference is used across multiple calls such as Find-package, Install-package, UnInstall-Package and Download-Package.
# The format of FastPackageReference needs to be consistent within your provider. It usually contains package Name, Version and Source.
# In the MyAlbum case, we choose the file full path just for demo purpose.
$swidObject = @{
FastPackageReference = $file.FullName;
Name = $file.Name;
Version = New-Object System.Version ("0.1"); # Note: You need to fill in a proper package version.
versionScheme = "MultiPartNumeric";
summary = "Add the summary of your package provider here";
Source = $location;
}
$sid = New-SoftwareIdentity @swidObject
Write-Output -InputObject $sid
}
}
}
# Optional function that downloads a remote package file to a local location. It is called for Save-Package.
# It is required to implement this function for the providers that support save-package. For example, save-package -Name Seattle -Path C:\ForSave\.
function Download-Package
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$FastPackageReference,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$Location
)
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Download-Package'))
Write-Debug -Message ($LocalizedData.FastPackageReference -f $fastPackageReference)
<#
You need to add code here in your real provider:
1. parse the FastPackageReference for package name, version, source etc.
2. Find the matched source from the registered ones
3. Use the Source to download packages
#>
Install-PackageUtility -FastPackageReference $fastPackageReference -Location $Location -Request $request
}
# It is required to implement this function for the providers that support install-package.
# for example, install-package -Name seattle -ProviderName myalbum -Source demo -Destination c:\myfolder
function Install-Package
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$fastPackageReference
)
Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Install-Package'))
Write-Debug -Message ($LocalizedData.FastPackageReference -f $fastPackageReference)
$path = Get-Path -Request $request
Install-PackageUtility -FastPackageReference $fastPackageReference -Location $path -Request $request
}
# A helper function for install-package and save-package
function Install-PackageUtility
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$FastPackageReference,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
$Location,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$Request
)
# Check the source location
if(-Not (Test-Path -Path $fastPackageReference))
{
ThrowError -ExceptionName "System.ArgumentException" `
-ExceptionMessage ($LocalizedData.PathNotFound -f ($fastPackageReference)) `
-ErrorId "PathNotFound" `
-CallerPSCmdlet $CallerPSCmdlet `
-ErrorCategory InvalidArgument `
-ExceptionObject $fastPackageReference
}
# Check the destination location
if(-Not (Test-Path -Path $Location))
{
New-Item -Path $Location -ItemType Directory -Force
}
# Get the cmdlet parameter values that were passed from the user
$force = $false
$options = $request.Options
if($options.ContainsKey('Force'))
{
$force = $options['Force']
}
Copy-Item -Path $fastPackageReference -Destination $Location -Force:$force -WhatIf:$false -Confirm:$false
$swidObject = @{
FastPackageReference = $fastPackageReference;
Name = [System.IO.Path]::GetFileName($fastPackageReference);
Version = New-Object System.Version ("0.1"); # Note: You need to fill in a proper package version
versionScheme = "MultiPartNumeric";
summary = "Summary of your package provider";
Source = [System.IO.Path]::GetDirectoryName($fastPackageReference)
}
$swidTag = New-SoftwareIdentity @swidObject
Write-Output -InputObject $swidTag
}
# It is required to implement this function for the providers that support UnInstall-Package.
# For example, UnInstall-Package -Name seattle -ProviderName myalbum -Destination c:\myfolder.
function UnInstall-Package
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$fastPackageReference
)
Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Uninstall-Package'))
Write-Debug -Message ($LocalizedData.FastPackageReference -f $fastPackageReference)
$fileFullName = $fastPackageReference
if(Test-Path -Path $fileFullName)
{
Remove-Item $fileFullName -Force -WhatIf:$false -Confirm:$false
$swidObject = @{
FastPackageReference = $fileFullName;
Name = [System.IO.Path]::GetFileName($fileFullName);
Version = New-Object System.Version ("0.1"); # Note: You need to fill in a proper package version
versionScheme = "MultiPartNumeric";
summary = "Summary of your package provider";
Source = [System.IO.Path]::GetDirectoryName($fileFullName)
}
$swidTag = New-SoftwareIdentity @swidObject
Write-Output -InputObject $swidTag
}
}
# Optional function that returns the packages that are installed. However it is required to implement this function for the providers
# that support Get-Package. It's also called during install-package.
# For example, Get-package -Destination c:\myfolder -ProviderName MyAlbum
function Get-InstalledPackage
{
[CmdletBinding()]
param
(
[Parameter()]
[string]
$Name,
[Parameter()]
[string]
$RequiredVersion,
[Parameter()]
[string]
$MinimumVersion,
[Parameter()]
[string]
$MaximumVersion
)
Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Get-InstalledPackage'))
#You can check the version here...
#<your code>
$fullPath = Get-Path -Request $request
if (Test-Path -Path $fullPath)
{
# Find the photos
$files = Get-ChildItem -Path $fullPath -Filter '*.png' -Recurse | `
Where-Object { ($_.PSIsContainer -eq $false) -and ( $_.Name -like "*$Name*") }
foreach($file in $files)
{
if($request.IsCanceled) { return }
$swidObject = @{
FastPackageReference = $file.FullName;
Name = $file.Name;
Version = New-Object System.Version ("0.1");
versionScheme = "MultiPartNumeric";
summary = "Summary of your package provider";
Source = $file.FullName;
}
$swidTag = New-SoftwareIdentity @swidObject
Write-Output -InputObject $swidTag
}
}
}
#endregion
#region Helper functions
# Get the package destination path
function Get-Path
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$Request
)
# Get the cmdlet parameter values that were passed from the user. Via Options, we can find out what's the installation path.
$options = $Request.Options
foreach( $o in $options.Keys )
{
Write-Debug ( "OPTION: {0} => {1}" -f ($o, $options[$o]) )
}
if($options -and $options.ContainsKey('Destination'))
{
$path = $($options['Destination'])
return $path
}
}
# Test if the $Name contains any wildcard characters
function Test-WildcardPattern
{
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
$Name
)
return [System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($Name)
}
# Find package source name from a given location
function Get-SourceName
{
[CmdletBinding()]
[OutputType("string")]
Param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$Location
)
Set-PackageSourcesVariable
foreach($source in $script:RegisteredPackageSources.Values)
{
if($source.SourceLocation -eq $Location)
{
return $source.Name
}
}
}
# Yield the package source to OneGet
function New-PackageSourceAndYield
{
param
(
[Parameter(Mandatory)]
$Source
)
# create a new package source
$src = New-PackageSource -Name $Source.Name `
-Location $Source.SourceLocation `
-Trusted $Source.Trusted `
-Registered $Source.Registered `
Write-Verbose ( $LocalizedData.PackageSourceDetails -f ($src.Name, $src.Location, $src.IsTrusted, $src.IsRegistered) )
# return the package source object.
Write-Output -InputObject $src
}
# Read the registered package sources from its configuration file
function Set-PackageSourcesVariable
{
param([switch]$Force)
if(-not $script:RegisteredPackageSources -or $Force)
{
if(Microsoft.PowerShell.Management\Test-Path $script:RegisteredPackageSourcesFilePath)
{
$script:RegisteredPackageSources = DeSerializePSObject -Path $script:RegisteredPackageSourcesFilePath
}
else
{
$script:RegisteredPackageSources = [ordered]@{}
}
}
}
# Read xml content to into an object
function DeSerializePSObject
{
[CmdletBinding(PositionalBinding=$false)]
Param
(
[Parameter(Mandatory=$true)]
$Path
)
# You can use import-clixml here. However this cmdlet is not available on Nano Server yet, so we choose PSSerializer to
# make the provider run on both full server and Nano Server.
$filecontent = Microsoft.PowerShell.Management\Get-Content -Path $Path
[System.Management.Automation.PSSerializer]::Deserialize($filecontent)
}
# Save the package source to the configuration file
function Save-PackageSources
{
if($script:RegisteredPackageSources)
{
if(-not (Microsoft.PowerShell.Management\Test-Path $script:LocalPath))
{
$null = Microsoft.PowerShell.Management\New-Item -Path $script:LocalPath `
-ItemType Directory `
-Force `
-ErrorAction SilentlyContinue `
-WarningAction SilentlyContinue `
-Confirm:$false -WhatIf:$false
}
# You can use export-clixml here. However this cmdlet is not available on Nano Server yet, so we choose PSSerializer to
# make the provider run on both full server and Nano Server.
Microsoft.PowerShell.Utility\Out-File `
-FilePath $script:RegisteredPackageSourcesFilePath `
-Force `
-InputObject ([System.Management.Automation.PSSerializer]::Serialize($script:RegisteredPackageSources))
}
}
# Validate versions
function Validate-VersionParameters
{
Param(
[Parameter()]
[String[]]
$Name,
[Parameter()]
[string]
$MinimumVersion,
[Parameter()]
[string]
$RequiredVersion,
[Parameter()]
[string]
$MaximumVersion,
[Parameter()]
[Switch]
$AllVersions
)
<#
Add code here to validate versions
#>
# Once we complete the validation, return the result. As a sample here, we assume we have passed the version checking
return $true
}
# Utility to throw an errorrecord
function ThrowError
{
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCmdlet]
$CallerPSCmdlet,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$ExceptionName,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$ExceptionMessage,
[System.Object]
$ExceptionObject,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$ErrorId,
[parameter(Mandatory = $true)]
[ValidateNotNull()]
[System.Management.Automation.ErrorCategory]
$ErrorCategory
)
$exception = New-Object $ExceptionName $ExceptionMessage;
$errorRecord = New-Object System.Management.Automation.ErrorRecord $exception, $ErrorId, $ErrorCategory, $ExceptionObject
$CallerPSCmdlet.ThrowTerminatingError($errorRecord)
}
#endregion