-
Notifications
You must be signed in to change notification settings - Fork 14
/
Get-SecureSetting.ps1
180 lines (154 loc) · 14.6 KB
/
Get-SecureSetting.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
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
function Get-SecureSetting
{
<#
.Synopsis
Gets encrypted settings stored in the registry
.Description
Gets secured user settings stored in the registry
.Example
Get-SecureSetting
.Example
Get-SecureSetting MySetting
.Example
Get-SecureSetting MySetting -Decrypt
.Example
Get-SecureSetting MySetting -ValueOnly
.Link
https://www.youtube.com/watch?v=0haXavQU_nY
.Link
Add-SecureSetting
.Link
Remove-SecureSetting
.Link
ConvertTo-SecureString
.Link
ConvertFrom-SecureString
#>
[OutputType('SecureSetting')]
param(
# The name of the secure setting
[Parameter(Position=0,ValueFromPipelineByPropertyName=$true)]
[String]
$Name,
# The type of the secure setting
[Parameter(Position=1,ValueFromPipelineByPropertyName=$true)]
[Type]
$Type,
# If set, will decrypt the setting value
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Switch]
$Decrypted,
# If set, will decrypt the setting value and return the data
[switch]
$ValueOnly
)
begin {
$getSecureSetting = {
$Obj = $_
$typeName = $_.pschildName
foreach ($propName in ($obj.psobject.properties | Select-Object -ExpandProperty Name)) {
if ('PSPath', 'PSParentPath', 'PSChildName', 'PSProvider' -contains $propName) {
$obj.psobject.properties.Remove($propname)
}
}
$Obj.psobject.properties |
ForEach-Object {
$secureSetting = New-Object PSObject
$null = $secureSetting.pstypenames.add('SecureSetting')
$secureSetting |
Add-Member NoteProperty Name $_.Name -PassThru |
Add-Member NoteProperty Type ($typename -as [Type]) -PassThru |
Add-Member NoteProperty EncryptedData $_.Value -PassThru
}
}
}
process {
# If Request and Response are present, Get-SecureSetting acts like Get-WebConfigurationSetting
if ($Request -and $Response -and -not $inPSNode) {
if ($Request -and $request.Params -and $request.Params['Path_Info']) {
$path ="$((Split-Path $request['Path_Info']))"
$webConfigStore = [Web.Configuration.WebConfigurationManager]::OpenWebConfiguration($path)
} else {
# Otherwise, use the global one
$webConfigStore = [Web.Configuration.WebConfigurationManager]::OpenWebConfiguration($null)
}
#endregion Load Config Store
if ($name) {
# Get the custom setting
$customSetting = $webConfigStore.AppSettings.Settings["$name"];
# If there is a value, return it.
if ($CustomSetting) {
$CustomSetting.Value
}
}
return
}
#region Create Registry Location If It Doesn't Exist
$regSubKey = if ($myInvocation.MyCommand.ScriptBlock.Module.Name) {
$myInvocation.MyCommand.ScriptBlock.Module.Name
} else {
"Pipeworks"
}
$registryPath = "HKCU:\Software\Start-Automating\$regSubKey"
$fullRegistryPath = "$registryPath\$($psCmdlet.ParameterSetName)"
if (-not (Test-Path $fullRegistryPath)) {
$null = New-Item $fullRegistryPath -Force
}
#endregion Create Registry Location If It Doesn't Exist
Get-ChildItem $registryPath |
Get-ItemProperty |
ForEach-Object $getSecureSetting |
Where-Object {
if ($psBoundParameters.Name -and $_.Name -notlike "$name*") { return }
if ($psBoundParameters.Type -and $_.Type -ne $Type) { return }
$true
} |
ForEach-Object -Begin {
$TempCredTable = @{}
} -Process {
if (-not ($decrypted -or $ValueOnly)) { return $_ }
#region Decrypt and Convert Output
$inputObject = $_
if ([Hashtable], [string] -contains $_.Type) {
# Create a credential to unpack it
$convertedAgain =
New-Object Management.Automation.PSCredential ' ', ($_.EncryptedData | ConvertTo-SecureString)
$decryptedValue= $convertedAgain.GetNetworkCredential().Password
if ($_.Type -eq [Hashtable]) {
$decryptedValue = . ([ScriptBlock]::Create($decryptedValue))
}
} elseif ($_.Type -eq [Security.SecureString]) {
$decryptedValue= ($_.EncryptedData | ConvertTo-SecureString)
} elseif ($_.Type -eq [Management.Automation.PSCredential]) {
# Create a credential to unpack the username, then create a credential with the unpacked password
$baseName = $_.Name -ireplace "_UserName", ""
if ($_.Name -like "*_UserName") {
$convertedAgain =
New-Object Management.Automation.PSCredential ' ', ($_.EncryptedData | ConvertTo-SecureString)
$decryptedValue= $convertedAgain.GetNetworkCredential().Password
$tempCredTable["UserName"] = $decryptedValue
} elseif ($_.Name -like "*_Password") {
$tempCredTable["Password"] = ($_.EncryptedData | ConvertTo-SecureString)
}
}
$null = $inputObject.psobject.properties.Remove('EncryptedData')
if ($inputObject.Name -notlike "*_UserName") {
if ($inputObject.Name -like "*_Password" -and $tempCredTable.UserName) {
$inputObject |
Add-Member NoteProperty Name $baseName -Force
$decryptedValue =New-Object Management.Automation.PSCredential $tempCredTable["UserName"], $tempCredTable["PassWord"]
}
$inputObject |
Add-Member NoteProperty DecryptedData $decryptedValue -PassThru |
ForEach-Object {
if ($ValueOnly) {
$_.DecryptedData
} else {
$_
}
}
}
#endregion Decrypt and Convert Output
}
}
}