-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGet-ContextInfo.ps1
More file actions
116 lines (97 loc) · 3.66 KB
/
Copy pathGet-ContextInfo.ps1
File metadata and controls
116 lines (97 loc) · 3.66 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
function Get-ContextInfo {
<#
.SYNOPSIS
Retrieves info about a context from a context vault.
.DESCRIPTION
Retrieves info about contexts directly from a ContextVault.
If no ID is specified, info on all contexts will be returned.
Wildcards are supported to match multiple contexts.
Only metadata (ID and Path) is returned without decrypting the context contents.
.EXAMPLE
Get-ContextInfo
Output:
```powershell
ID Vault
-- -----
MySettings MyVault
MyConfig MyVault
MySecret MyVault
Data MyVault
PSModule.GitHub MyVault
```
Retrieves all contexts from the context vault (directly from disk).
.EXAMPLE
Get-ContextInfo -ID 'MySecret'
Output:
```powershell
ID : MySecret
Path : ...\3e223259-f242-4e97-91c8-f0fd054cfea7.json
```
Retrieves the context called 'MySecret' from the context vault (directly from disk).
.EXAMPLE
'My*' | Get-ContextInfo
Output:
```powershell
ID Vault
-- -----
MyConfig MyVault
MySecret MyVault
MySettings MyVault
```
Retrieves all contexts that start with 'My' from the context vault (directly from disk).
.OUTPUTS
[ContextInfo]
.NOTES
Returns a list of context information matching the specified ID or all contexts if no ID is specified.
Each context object contains its ID and corresponding path to where the context is stored on disk.
.LINK
https://psmodule.io/Context/Functions/Get-ContextInfo/
#>
[OutputType([ContextInfo])]
[CmdletBinding()]
param(
# The name of the context to retrieve from the vault. Supports wildcards.
[Parameter()]
[SupportsWildcards()]
[string[]] $ID = '*',
# The name of the vault to retrieve context info from. Supports wildcards.
[Parameter()]
[string[]] $Vault = '*'
)
begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Begin"
}
process {
$vaults = foreach ($vaultName in $Vault) {
Get-ContextVault -Name $vaultName -ErrorAction Stop
}
Write-Verbose "[$stackPath] - Found $($vaults.Count) vault(s) matching '$($Vault -join ', ')'."
$files = foreach ($vaultObject in $vaults) {
Get-ChildItem -Path $vaultObject.Path -Filter *.json -File
}
Write-Verbose "[$stackPath] - Found $($files.Count) context file(s) in vault(s)."
foreach ($file in $files) {
# Use non-locking file reading to allow concurrent access
try {
$content = Get-ContentNonLocking -Path $file.FullName
$contextInfo = $content | ConvertFrom-Json
} catch {
Write-Warning "[$stackPath] - Error reading context file '$($file.FullName)': $($_.Exception.Message)"
continue
}
if ($VerbosePreference -eq 'Continue') {
Write-Verbose "[$stackPath] - Processing file: $($file.FullName)"
$contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose "[$stackPath] $_" }
}
foreach ($IDItem in $ID) {
if ($contextInfo.ID -like $IDItem) {
[ContextInfo]::new($contextInfo)
}
}
}
}
end {
Write-Debug "[$stackPath] - End"
}
}