-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-ConfigData.ps1
134 lines (117 loc) · 3.13 KB
/
Get-ConfigData.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
<#
.SYNOPSIS
Get data from a JSON config file
.PARAMETER Name
Name of the config data to get. Supports tab completion
.PARAMETER AsSecureString
Return the encrypted data as a SecureString
.PARAMETER Decrypt
Return encrypted data in clear text
.PARAMETER Quiet
Don't kick out a Warning message if not found
.PARAMETER Path
Path to the config file
.EXAMPLE
Get-ConfigData Key
.OUTPUTS
Object from config file, or $null
#>
function Get-ConfigData
{
[CmdletBinding(PositionalBinding=$false)] # PositionalBinding=$false allows us to use dynamic Name w/o -Name
[OutputType([string],[PSCustomObject])]
param(
[switch] $AsSecureString,
[switch] $Decrypt,
[switch] $Quiet,
[switch] $SkipNameValidate,
[string] $Path
)
dynamicParam {
makeDynamicParam -ParameterName "Name" -ValidateSetScript {
if ( !((Test-Path variable:NoNameValidate) -and $SkipNameValidate))
{
if (!((Test-Path variable:Path) -and $Path))
{
$Path = $null # can't pass in undefined path
}
Find-ConfigData -Path $Path
}
} -Mandatory -ValueFromPipeline
}
process
{
Write-Verbose "Path is $Path"
Set-StrictMode -Version Latest
$Name = $psboundparameters["Name"]
$Path = Get-ConfigDataPath $Path
if ( -not (Test-Path $Path -PathType Leaf))
{
throw "Path $Path not found"
}
Write-Verbose "Getting $Name from $Path"
$object = Get-Content $path -Raw | ConvertFrom-Json
$value = $null
$member = Get-Member -InputObject $object -MemberType NoteProperty | Where-Object Name -eq $Name | Select-Object -ExpandProperty name
if ( !$member )
{
if ( !$Quiet )
{
Write-Warning "Didn't find value named $name in $path"
}
}
else
{
$value = $object.$member
if ( $PSVersionTable.PSVersion.Major -gt 5 -and -not $IsWindows )
{
$decryptedtString = $false # Core 2.0 doesn't support encrypt/decrypt
if ( $AsSecureString )
{
Write-Warning "AsSecureString not supported in PS Core"
return ""
}
}
$value = ConvertFrom-Json $value
$isSecureString = [bool](Get-Member -InputObject $value -Name "Secure-String" -MemberType NoteProperty )
$isEncryptedObject = [bool](Get-Member -InputObject $value -Name "Encrypted-Object" -MemberType NoteProperty )
$isEncryptedString = [bool](Get-Member -InputObject $value -Name "Encrypted-String" -MemberType NoteProperty )
Write-Verbose "isSecureString = $isSecureString isEncryptedObject = $isEncryptedObject"
if ( $isSecureString -or $isEncryptedObject -or $isEncryptedString )
{
if ( $isEncryptedObject )
{
$value = $value."Encrypted-Object"
}
elseif ( $isEncryptedString )
{
$value = $value."Encrypted-String"
}
else
{
$value = $value."Secure-String"
}
Write-Verbose "Value is $value"
$secureString = $value | ConvertTo-SecureString
if ( $Decrypt )
{
$decryptedtString = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString))
if ( $isEncryptedObject )
{
$value = ConvertFrom-Json $decryptedtString
}
else
{
$value = $decryptedtString
}
}
else
{
$value = $secureString
}
}
$value
}
}
}
New-Alias -Name gcd -Value Get-ConfigData