Repository navigation
Expand file tree
/
Copy pathConfiguration.psm1
More file actions
653 lines (595 loc) · 26.3 KB
/
Copy pathConfiguration.psm1
File metadata and controls
653 lines (595 loc) · 26.3 KB
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
# We're not using Requires because it just gets in the way on PSv2
#!Requires -Modules ModuleInfo, LocalStorage
# FULL # BEGIN FULL: Don't include this in the installer script
$PoshCodeModuleRoot = Get-Variable PSScriptRoot -ErrorAction SilentlyContinue | ForEach-Object { $_.Value }
if(!$PoshCodeModuleRoot) {
$PoshCodeModuleRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
}
. $PoshCodeModuleRoot\Constants.ps1
# FULL # END FULL
########################################################################
## Copyright (c) 2013 by Joel Bennett, all rights reserved.
## Free for use under MS-PL, MS-RL, GPL 2, or BSD license. Your choice.
########################################################################
## Configuration.psm1 defines the Get/Set functionality for ConfigData
## It also includes Get-SpecialFolder for resolving special folder paths
$Script:SpecialFolderNames = @([System.Environment+SpecialFolder].GetFields("Public,Static") | ForEach-Object { $_.Name }) + @("PSHome") | Sort-Object
function Get-SpecialFolder {
#.Synopsis
# Gets the current value for a well known special folder
[CmdletBinding()]
param(
# The name of the Path you want to fetch (supports wildcards).
# From the list: AdminTools, ApplicationData, CDBurning, CommonAdminTools, CommonApplicationData, CommonDesktopDirectory, CommonDocuments, CommonMusic, CommonOemLinks, CommonPictures, CommonProgramFiles, CommonProgramFilesX86, CommonPrograms, CommonStartMenu, CommonStartup, CommonTemplates, CommonVideos, Cookies, Desktop, DesktopDirectory, Favorites, Fonts, History, InternetCache, LocalApplicationData, LocalizedResources, MyComputer, MyDocuments, MyMusic, MyPictures, MyVideos, NetworkShortcuts, Personal, PrinterShortcuts, ProgramFiles, ProgramFilesX86, Programs, PSHome, Recent, Resources, SendTo, StartMenu, Startup, System, SystemX86, Templates, UserProfile, Windows
[ValidateScript({
$Name = $_
$Names =
if($Script:SpecialFolderNames -like $Name) {
return $true
} else {
throw "Cannot convert Path, with value: `"$Name`", to type `"System.Environment+SpecialFolder`": Error: `"The identifier name $Name is noe one of $($Names -join ', ')"
}
})]
[String]$Path = "*",
# If set, returns a hashtable of folder names to paths
[Switch]$Value
)
$Names = $Script:SpecialFolderNames -like $Path
if(!$Value) {
$return = @{}
}
foreach($name in $Names) {
$result = $(
if($name -eq "PSHome") {
$PSHome
} else {
[Environment]::GetFolderPath($name)
}
)
if($result) {
if($Value) {
Write-Output $result
} else {
$return.$name = $result
}
}
}
if(!$Value) {
Write-Output $return
}
}
function Select-ModulePath {
#.Synopsis
# Interactively choose (and validate) a folder from the Env:PSModulePath
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
param(
# The folder to install to. This folder should be one of the ones in the PSModulePath, NOT a subfolder.
$InstallPath
)
end {
$ChoicesWithHelp = @()
[Char]$Letter = "D"
$default = -1
$index = -1
$common = -1
# Suppress error when running in remote sessions by making sure $PROFILE is defined
if(!$PROFILE) { $PROFILE = Join-Path (Get-SpecialFolder MyDocuments) "WindowsPowerShell\Profile.ps1" }
switch -Wildcard ($Env:PSModulePath -split ";" | ? {$_}) {
"${PSHome}*" {
##### We do not support installing to the System location. #####
#$index++
#$ChoicesWithHelp += New-Object System.Management.Automation.Host.ChoiceDescription "S&ystem", $_
continue
}
"$(Split-Path $PROFILE)*" {
$index++
$ChoicesWithHelp += New-Object System.Management.Automation.Host.ChoiceDescription "&Profile", $_
$default = $index
continue
}
"$(Join-Path ([Environment]::GetFolderPath("ProgramFiles")) WindowsPowerShell\Modules*)" {
$index++
$ChoicesWithHelp += New-Object System.Management.Automation.Host.ChoiceDescription $(if($common -lt 0){"&Common"}elseif($common -lt 1){"C&ommon"}elseif($common -lt 2){"Co&mmon"}else{"Commo&n"}), $_
$common++
if($Default -lt 0){$Default = $index}
continue
}
"$(Join-Path ([Environment]::GetFolderPath("ProgramFiles")) Microsoft\Windows\PowerShell\Modules*)" {
$index++
$ChoicesWithHelp += New-Object System.Management.Automation.Host.ChoiceDescription $(if($common -lt 0){"&Common"}elseif($common -lt 1){"C&ommon"}elseif($common -lt 2){"Co&mmon"}else{"Commo&n"}), $_
$common++
if($Default -lt 0){$Default = $index}
continue
}
"$(Join-Path ([Environment]::GetFolderPath("CommonProgramFiles")) Modules)*" {
$index++
$ChoicesWithHelp += New-Object System.Management.Automation.Host.ChoiceDescription $(if($common -lt 0){"&Common"}elseif($common -lt 1){"C&ommon"}elseif($common -lt 2){"Co&mmon"}else{"Commo&n"}), $_
$common++
if($Default -lt 0){$Default = $index}
continue
}
"$(Join-Path ([Environment]::GetFolderPath("CommonDocuments")) Modules)*" {
$index++
$ChoicesWithHelp += New-Object System.Management.Automation.Host.ChoiceDescription $(if($common -lt 0){"&Common"}elseif($common -lt 1){"C&ommon"}elseif($common -lt 2){"Co&mmon"}else{"Commo&n"}), $_
$common++
if($Default -lt 0){$Default = $index}
continue
}
"$([Environment]::GetFolderPath("MyDocuments"))*" {
$index++
$ChoicesWithHelp += New-Object System.Management.Automation.Host.ChoiceDescription "&MyDocuments", $_
if($Default -lt 0){$Default = $index}
continue
}
default {
$index++
$Key = $_ -replace [regex]::Escape($Env:USERPROFILE),'~' -replace "((?:[^\\]*\\){2}).+((?:[^\\]*\\){2})",'$1...$2'
$ChoicesWithHelp += New-Object System.Management.Automation.Host.ChoiceDescription "&$Letter $Key", $_
$Letter = 1 + $Letter
continue
}
}
# Let's make sure they have at least one of the "Common" locations:
if($common -lt 0) {
$index++
$ChoicesWithHelp += New-Object System.Management.Automation.Host.ChoiceDescription "&Common", (Join-Path ([Environment]::GetFolderPath("ProgramFiles")) WindowsPowerShell\Modules)
}
# And we always offer the "Other" location:
$index++
$ChoicesWithHelp += New-Object System.Management.Automation.Host.ChoiceDescription "&Other", "Type in your own path!"
while(!$InstallPath -or !(Test-Path $InstallPath)) {
if($InstallPath -and !(Test-Path $InstallPath)){
if($PSCmdlet.ShouldProcess(
"Verifying module install path '$InstallPath'",
"Create folder '$InstallPath'?",
"Creating Module Install Path" )) {
$null = New-Item -Type Directory -Path $InstallPath -Force -ErrorVariable FailMkDir
## Handle the error if they asked for -Common and don't have permissions
if($FailMkDir -and @($FailMkDir)[0].CategoryInfo.Category -eq "PermissionDenied") {
Write-Warning "You do not have permission to install a module to '$InstallPath\$ModuleName'. You may need to be elevated. (Press Ctrl+C to cancel)"
}
}
}
if(!$InstallPath -or !(Test-Path $InstallPath)){
$Answer = $Host.UI.PromptForChoice(
"Please choose an install path.",
"Choose a Module Folder (use ? to see the full paths)",
([System.Management.Automation.Host.ChoiceDescription[]]$ChoicesWithHelp),
$Default)
if($Answer -ge $index) {
$InstallPath = Read-Host ("You should pick a path that's already in your PSModulePath. " +
"To choose again, press Enter.`n" +
"Otherwise, type the path for a 'Modules' folder you want to create")
} else {
$InstallPath = $ChoicesWithHelp[$Answer].HelpMessage
}
}
}
return $InstallPath
}
}
function Test-ExecutionPolicy {
#.Synopsis
# Validate the ExecutionPolicy
param()
$Policy = Get-ExecutionPolicy
if(([Microsoft.PowerShell.ExecutionPolicy[]]"Restricted","Default") -contains $Policy) {
$Warning = "Your execution policy is $Policy, so you will not be able import script modules."
} elseif(([Microsoft.PowerShell.ExecutionPolicy[]]"AllSigned") -eq $Policy) {
$Warning = "Your execution policy is $Policy, if modules are not signed, you won't be able to import them."
}
if($Warning) {
Write-Warning ("$Warning`n" +
"You may want to change your execution policy to RemoteSigned, Unrestricted or even Bypass.`n" +
"`n" +
" PS> Set-ExecutionPolicy RemoteSigned`n" +
"`n" +
"For more information, read about execution policies by executing:`n" +
" `n" +
" PS> Get-Help about_execution_policies`n")
} elseif(([Microsoft.PowerShell.ExecutionPolicy]"Unrestricted") -eq $Policy) {
Write-Host "Your execution policy is $Policy and should be fine. Note that modules flagged as internet may still cause warnings."
} elseif(([Microsoft.PowerShell.ExecutionPolicy]"RemoteSigned") -contains $Policy) {
Write-Host "Your execution policy is $Policy and should be fine. Note that modules flagged as internet will not load if they're not signed."
}
}
# FULL # BEGIN FULL: These cmdlets are only necessary in the full version of the module
function Get-ConfigData {
#.Synopsis
# Gets the modulename.ini settings as a hashtable
#.Description
# Parses the non-comment lines in the config file as a simple hashtable,
# parsing it as string data, and replacing {SpecialFolder} paths
[CmdletBinding(DefaultParameterSetname="FromFile")]
param()
end {
$Results = Import-LocalStorage $PSScriptRoot UserSettings.psd1
# Our ConfigData has InstallPaths which may use tokens:
foreach($Key in $($Results.InstallPaths.Keys)) {
$Results.InstallPaths.$Key = $(
foreach($StringData in @($Results.InstallPaths.$Key)) {
$Paths = [Regex]::Matches($StringData, "{(?:$($Script:SpecialFolderNames -Join "|"))}")
for($i = $Paths.Count - 1; $i -ge 0; $i--) {
if($Path = Get-SpecialFolder $Paths[$i].Value.Trim("{}") -Value) {
$StringData = $StringData.Remove($Paths[$i].Index,$Paths[$i].Length).Insert($Paths[$i].Index, $Path)
break
}
}
$StringData
}
)
}
# Our ConfigData has Repositories which may use tokens in their roots
# The Repositories has to be an array:
$Results.Repositories = @(
foreach($Repo in $Results.Repositories) {
foreach($Key in @($Repo.Keys)) {
$Repo.$Key = $(
foreach($StringData in @($Repo.$Key)) {
$Paths = [Regex]::Matches($StringData, "{(?:$($Script:SpecialFolderNames -Join "|"))}")
for($i = $Paths.Count - 1; $i -ge 0; $i--) {
if($Path = Get-SpecialFolder $Paths[$i].Value.Trim("{}") -Value) {
$StringData = $StringData.Remove($Paths[$i].Index,$Paths[$i].Length).Insert($Paths[$i].Index, $Path)
break
}
}
$StringData
}
)
}
$Repo
}
)
return $Results
}
}
function Set-ConfigData {
#.Synopsis
# Updates the config file with the specified hashtable
[CmdletBinding()]
param(
# The config hashtable to save
[Parameter(ValueFromPipeline=$true, Position=0)]
[Hashtable]$ConfigData
)
end {
# When serializing the ConfigData we want to tokenize the path
# So that it will be user-agnostic
$table = Get-SpecialFolder
$table = $table.GetEnumerator() | Sort-Object Value -Descending
# Our ConfigData has InstallPaths and Repositories
# We'll explicitly save just those:
$SaveData = @{ InstallPaths = @{}; Repositories = @{} }
# Our ConfigData has InstallPaths which may use tokens:
foreach($Key in $($ConfigData.InstallPaths.Keys)) {
$SaveData.InstallPaths.$Key = $ConfigData.InstallPaths.$Key
foreach($kvPath in $table) {
if($ConfigData.InstallPaths.$Key -like ($kvPath.Value +"*")) {
$SaveData.InstallPaths.$Key = $ConfigData.InstallPaths.$Key -replace ([regex]::Escape($kvPath.Value)), "{$($kvPath.Key)}"
break
}
}
}
# Our ConfigData has Repositories which may use tokens in their roots
# The Repositories has to be an array:
$SaveData.Repositories = @(
foreach($Repo in $ConfigData.Repositories) {
foreach($Key in @($Repo.Keys)) {
foreach($kvPath in $table) {
if($Repo.$Key -like ($kvPath.Value +"*")) {
$Repo.$Key = $Repo.$Key -replace ([regex]::Escape($kvPath.Value)), "{$($kvPath.Key)}"
break
}
}
}
$Repo
}
)
$ConfigString = "# You can edit this file using the ConfigData commands: Get-ConfigData and Set-ConfigData`n" +
"# For a list of valid {SpecialFolder} tokens, run Get-SpecialFolder`n" +
"# Note that the default InstallPaths here are the ones recommended by Microsoft:`n" +
"# http://msdn.microsoft.com/en-us/library/windows/desktop/dd878350`n" +
"#`n" +
"# Repositories: must be an array of hashtables with Type and Root`n" +
"# Optionally, Repositories may have a name (useful for filtering Find-Module)`n" +
"# and may include settings/parameters for the Repository's FindModule command`n"
Export-LocalStorage -Module $PSScriptRoot -Name UserSettings.psd1 -InputObject $ConfigData -CommentHeader $ConfigString
}
}
function Test-ConfigData {
#.Synopsis
# Validate and configure the module installation paths
[CmdletBinding()]
param(
# A Name=Path hashtable containing the paths you want to use in your configuration
$ConfigData = $(Get-ConfigData)
)
foreach($path in @($ConfigData.InstallPaths.Keys)) {
$name = $path -replace 'Path$'
$folder = $ConfigData.$path
do {
## Create the folder, if necessary
if(!(Test-Path $folder)) {
Write-Warning "The $name module location does not exist. Please validate:"
$folder = Read-Host "Press ENTER to accept the current value:`n`t$($ConfigData.$path)`nor type a new path"
if([string]::IsNullOrWhiteSpace($folder)) { $folder = $ConfigData.$path }
if(!(Test-Path $folder)) {
$CP, $ConfirmPreference = $ConfirmPreference, 'Low'
if($PSCmdlet.ShouldContinue("The folder '$folder' does not exist, do you want to create it?", "Configuring <$name> module location:")) {
$ConfirmPreference = $CP
if(!(New-Item $folder -Type Directory -Force -ErrorAction SilentlyContinue -ErrorVariable fail))
{
Write-Warning ($fail.Exception.Message + "`nThe $name Location path '$folder' couldn't be created.`n`nYou may need to be elevated.`n`nPlease enter a new path, or press Ctrl+C to give up.")
}
}
$ConfirmPreference = $CP
}
}
## Note: PSModulePath entries don't necessarily exist
[string[]]$PSModulePaths = $Env:PSModulePath -split ";" #| Convert-Path -ErrorAction 0
## Add it to the PSModulePath, if necessary
if((Test-Path $folder) -and ($PSModulePaths -notcontains (Convert-Path $folder))) {
$folder = Convert-Path $folder
$CP, $ConfirmPreference = $ConfirmPreference, 'Low'
if($PSCmdlet.ShouldContinue("The folder '$folder' is not in your PSModulePath, do you want to add it?", "Configuring <$name> module location:")) {
$ConfirmPreference = $CP
# Global and System paths need to go in the Machine registry to work properly
if("Global","System","Common" -contains $name) {
try {
$PsMP = [System.Environment]::GetEnvironmentVariable("PSModulePath", "Machine") + ";" + $Folder
$PsMP = ($PsMP -split ";" | Where-Object { $_ } | Select-Object -Unique) -Join ";"
[System.Environment]::SetEnvironmentVariable("PSModulePath",$PsMP,"Machine")
$Env:PSModulePath = ($PSModulePaths + $folder) -join ";"
}
catch [System.Security.SecurityException]
{
Write-Warning ($_.Exception.Message + " The $name path '$folder' couldn't be added to your Local Machine PSModulePath.")
if($PSCmdlet.ShouldContinue("Do you want to store the path '$folder' in your <User> PSModulePath instead?", "Configuring <$name> module location:")) {
try {
$PsMP = [System.Environment]::GetEnvironmentVariable("PSModulePath", "User") + ";" + $Folder
$PsMP = ($PsMP -split ";" | Where-Object { $_ } | Select-Object -Unique) -Join ";"
[System.Environment]::SetEnvironmentVariable("PSModulePath", $PsMP, "User")
$Env:PSModulePath = ($PSModulePaths + $folder) -join ";"
Write-Host "Added '$folder' to your User PSModulePath instead."
}
catch [System.Security.SecurityException]
{
Write-Warning ($_.Exception.Message + " The $name path '$folder' couldn't be permanently added to your User PSModulePath. Adding for this session anyway.")
$Env:PSModulePath = ($PSModulePaths + $folder) -join ";"
}
}
}
} else {
try {
$PsMP = [System.Environment]::GetEnvironmentVariable("PSModulePath", "User") + ";" + $Folder
$PsMP = ($PsMP -split ";" | Where-Object { $_ } | Select-Object -Unique) -Join ";"
[System.Environment]::SetEnvironmentVariable("PSModulePath", $PsMP, "User")
$Env:PSModulePath = ($PSModulePaths + $folder) -join ";"
}
catch [System.Security.SecurityException]
{
Write-Warning ($_.Exception.Message + " The $name path '$folder' couldn't be permanently added to your User PSModulePath. Adding for this session anyway.")
$Env:PSModulePath = ($PSModulePaths + $folder) -join ";"
}
}
}
$ConfirmPreference = $CP
}
} while(!(Test-Path $folder))
$ConfigData.$path = $folder
}
# If you pass in a Hashtable, you get a Hashtable back
if($PSBoundParameters.ContainsKey("ConfigData")) {
Write-Output $ConfigData
# Otherwise, we set it back where we got it from!
} else {
Set-ConfigData -ConfigData $ConfigData
}
}
# These are special functions just for saving in the AppData folder...
function Get-LocalStoragePath {
#.Synopsis
# Gets the LocalApplicationData path for the specified company\module
#.Description
# Appends Company\Module to the LocalApplicationData, and ensures that the folder exists.
param(
# The name of the module you want to access storage for (defaults to SplunkStanzaName)
[Parameter(Position=0)]
[ValidateScript({
$invalid = $_.IndexOfAny([IO.Path]::GetInvalidFileNameChars())
if($invalid -eq -1){
return $true
} else {
throw "Invalid character in Module Name '$_' at $invalid"
}
})]
[string]$Module = $SplunkStanzaName,
# The name of a "company" to use in the storage path (defaults to "PowerShell Package Manager")
[Parameter(Position=1)]
[ValidateScript({
$invalid = $_.IndexOfAny([IO.Path]::GetInvalidFileNameChars())
if($invalid -eq -1){
return $true
} else {
throw "Invalid character in Company Name '$_' at $invalid"
}
})]
[string]$Company = "PowerShell Package Manager"
)
end {
if(!($path = $SplunkCheckpointPath)) {
$path = Join-Path ([Environment]::GetFolderPath("LocalApplicationData")) $Company
}
$path = Join-Path $path $Module
if(!(Test-Path $path -PathType Container)) {
$null = New-Item $path -Type Directory -Force
}
Write-Output $path
}
}
function Export-LocalStorage {
#.Synopsis
# Saves the object to local storage with the specified name
#.Description
# Persists objects to disk using Get-LocalStoragePath and Export-Metadata
param(
# A unique valid module name to use when persisting the object to disk
[Parameter(Mandatory=$true, Position=0)]
[ValidateScript({
$invalid = $_.IndexOfAny([IO.Path]::GetInvalidPathChars())
if($invalid -eq -1){
return $true
} else {
throw "Invalid character in Module Name '$_' at $invalid"
}
})]
[string]$Module,
# A unique object name to use when persisting the object to disk
[Parameter(Mandatory=$true, Position=1)]
[ValidateScript({
$invalid = $_.IndexOfAny([IO.Path]::GetInvalidFileNameChars())
if($invalid -eq -1){
return $true
} else {
throw "Invalid character in Object Name '$_' at $invalid"
}
})]
[string]$Name,
# The scope to store the data in. Defaults to storing in the ModulePath
[ValidateSet("Module", "User")]
$Scope,
# comments to place on the top of the file (to explain it's settings)
[string[]]$CommentHeader,
# The object to persist to disk
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=10)]
$InputObject
)
begin {
$invalid = $Module.IndexOfAny([IO.Path]::GetInvalidFileNameChars())
if(($Scope -ne "User") -and $invalid -and (Test-Path $Module))
{
$ModulePath = Resolve-Path $Module
}
elseif($Scope -eq "Module")
{
$Module = Split-Path $Module -Leaf
$ModulePath = Read-Module $Module -ListAvailable | Select -Expand ModuleBase -First 1
}
# Scope -eq "User"
if(!$ModulePath -or !(Test-Path $ModulePath)) {
$Module = Split-Path $Module -Leaf
$ModulePath = Get-LocalStoragePath $Module
if(!(Test-Path $ModulePath) -and ($Scope -ne "Module")) {
$Null = New-Item -ItemType Directory $ModulePath
}
}
if(!(Test-Path $ModulePath)) {
Write-Error "The folder for storage doesn't exist: $ModulePath"
}
$ModulePath = Resolve-Path $ModulePath -ErrorAction Stop
# Make sure it has a PSD1 extension
if($Name -notmatch '.*\.psd1$') {
$Name = "${Name}.psd1"
}
$Path = Join-Path $ModulePath $Name
if($PSBoundParameters.ContainsKey("InputObject")) {
Write-Verbose "Clean Export"
Write-Verbose ""
Export-Metadata -Path $Path -InputObject $InputObject -CommentHeader $CommentHeader
$Output = $null
} else {
$Output = @()
}
}
process {
if($Output) {
$Output += $InputObject
}
}
end {
if($Output) {
Write-Verbose "Tail Export"
# Avoid arrays when they're not needed:
if($Output.Count -eq 1) { $Output = $Output[0] }
Export-Metadata -Path $Path -InputObject $Output -CommentHeader $CommentHeader
}
}
}
function Import-LocalStorage {
#.Synopsis
# Loads an object with the specified name from local storage
#.Description
# Retrieves objects from disk using Get-LocalStoragePath and Import-CliXml
param(
# A unique valid module name to use when persisting the object to disk
[Parameter(Mandatory=$true, Position=0)]
[string]$Module,
# A unique object name to use when persisting the object to disk
[Parameter(Position=1)]
[ValidateScript({
$invalid = $_.IndexOfAny([IO.Path]::GetInvalidPathChars())
if($invalid -eq -1){
return $true
} else {
throw "Invalid character in Object Name '$_' at $invalid"
}
})]
[string]$Name = '*',
# The scope to store the data in. Defaults to storing in the ModulePath
[ValidateSet("Module", "User")]
$Scope,
# A default value (used in case there's an error importing):
[Parameter()]
[Object]$DefaultValue
)
end {
$invalid = $Module.IndexOfAny([IO.Path]::GetInvalidFileNameChars())
if(($Scope -ne "User") -and $invalid -and (Test-Path $Module))
{
$ModulePath = Resolve-Path $Module
}
elseif($Scope -eq "Module")
{
$Module = Split-Path $Module -Leaf
$ModulePath = Read-Module $Module -ListAvailable | Select -Expand ModuleBase -First 1
}
# Scope -eq "User"
if(!$ModulePath -or !(Test-Path $ModulePath)) {
$Module = Split-Path $Module -Leaf
$ModulePath = Get-LocalStoragePath $Module
if(!(Test-Path $ModulePath) -and ($Scope -ne "Module")) {
$Null = New-Item -ItemType Directory $ModulePath
}
}
if(!(Test-Path $ModulePath)) {
Write-Error "The folder for storage doesn't exist: $ModulePath"
}
# Make sure it has a PSD1 extension
if($Name -notmatch '.*\.psd1$') {
$Name = "${Name}.psd1"
}
$Path = Join-Path $ModulePath $Name
try {
$Path = Resolve-Path $Path -ErrorAction Stop
if(@($Path).Count -gt 1) {
$Output = @{}
foreach($Name in $Path) {
$Key = Split-Path $Name -Leaf
$Output.$Key = Import-Metadata -Path $Name
}
} else {
Import-Metadata -Path $Path
}
} catch {
if($PSBoundParameters.ContainsKey("DefaultValue")) {
Write-Output $DefaultValue
} else {
throw
}
}
}
}
Export-ModuleMember -Function Import-LocalStorage, Export-LocalStorage, Get-LocalStoragePath,
Get-SpecialFolder, Select-ModulePath, Test-ExecutionPolicy,
Get-ConfigData, Set-ConfigData, Test-ConfigData
# FULL # END FULL