-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_plain_text.ps1
More file actions
38 lines (34 loc) · 1.68 KB
/
convert_plain_text.ps1
File metadata and controls
38 lines (34 loc) · 1.68 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
# origin: https://pscustomobject.github.io/powershell/functions/PowerShell-SecureString-To-String/
param(
[string]$example = 'lorem ipsum',
[switch]$credentials,
[string]$user = $null
)
[securestring]$securedata | out-null
if ([bool]$psboundparameters['credentials'].ispresent) {
if ($user -eq $null -or $user -eq '') {
$user = "${env:USERDOMAIN}\${env:USERNAME}"
}
# step 1. Conllect credential as SecureString
$credential = get-credential -username $user -message 'enter credential'
[System.Net.NetworkCredential] $networkCredential = $credential.GetNetworkCredential()
write-output ('plain text: {0}' -f $networkCredential.Password )
# usually simply as
$data = $credential.GetNetworkCredential().password
# note that the plain text password is returned by calling to '.GetNetworkCredential()'
# see also
# https://www.py4u.net/discuss/1760454
# https://docs.microsoft.com/en-us/dotnet/api/system.net.networkcredential.-ctor?view=netframework-4.5
write-output write-output ('plain text: {0}' -f $data)
$securedata = $credential.password
} else {
# step 1. Convert plain text to SecureString
$securedata = ConvertTo-SecureString $example -AsPlainText -Force
}
$result = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securedata))
# NOTE: using ConvertTo-SecureString with the above snippet will lead to an error
# it is for storing in files
# https://stackoverflow.com/questions/28352141/convert-a-secure-string-to-plain-text
# see also vendor documentation:
# https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.securestringtobstr?view=netframework-4.8
write-output $result