Passwörter in PowerShell Skripten benutzen – Die Entschlüsselung

The estimated reading time 2 minutes

HINWEIS: bitte auf jeden Fall meine anderen beiden Artikel Passwörter in PowerShell Skripten benutzen – Prolog und Passwörter in PowerShell Skripten benutzen Verschlüsselung anschauen.

Nachdem man nun beide Dateien erstellt hat, gibt es natürlich auch die Möglichkeit das Passwort wieder zurückrechnen zu lassen und dieses dann auszugeben.
Zunächst wird das Passwort wieder in einen „Secure.String“ konvertiert, sodass die PowerShell damit arbeiten kann.

DOWNLOAD password reconvert script

Das Skript unterstützt auch allgemeine Parameter, speziell den „verbose“ Parameter.

Grundlagen:

mit diesen Linien können die beiden Dateien wieder zu einem Passwort zusammengefügt werden.

$key = (Get-Content "C:\temp\credtest\aeskey.key")
$password = Get-Content "C:\temp\credtest\password.txt" | ConvertTo-SecureString -Key $key
$temp = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($temp)
$PlainPassword

Funktion Het-ClearPassword

function Get-ClearPassword {
    
    [CmdletBinding(SupportsShouldProcess = $True)]
    param (
        [Parameter(Mandatory= $true)]
        [string]
        $credfile,
        [Parameter(Mandatory= $true)]
        [string]
        $encryptfile,
        [Parameter(Mandatory = $false)]
        [ValidateSet("YES","NO")]
        [string]$StorePWClipboard
        )
        #whatif case
        If ($WhatIfPreference) {
            Write-Verbose "Getting content from file: $encryptfile"
            Write-output "password = Get-Content "$credfile" | ConvertTo-SecureString -Key (Get-Content "$encryptfile")"
            Write-Verbose "Showing plain password in console"
            Write-Output "plain password:<< SHOWING YOUR PLAIN TRANSLATED PASSWORD >>"
            Write-Verbose "Clearing variable with plain password"
        }
        else {
            Write-Verbose "Getting content from file: $encryptfile"
            #loading key
            $key = (Get-Content "$encryptfile")
            Write-Verbose "Encypting $credfile with encryption file $encryptfile to secure string."  
            #loading password
            $password = Get-Content "$credfile" | ConvertTo-SecureString -Key $key
            #see also https://techibee.com/powershell/convert-system-security-securestring-to-plain-text-using-powershell/2599
            Write-Verbose "Converting securestring to plain password"
            #converting securestring back to plain
            $temp = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
            $PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($temp)
            Write-Verbose "Showing plain password in console"
            Write-Output "plain password:<< $PlainPassword >>"
            #storing password in clipboard
            if ($StorePWClipboard -eq "YES")
            {
                #https://www.powershellmagazine.com/2013/11/13/pstip-clear-clipboard-content/
                Set-Clipboard -Value "$PlainPassword"
                Write-Verbose "Clearing variable with plain password"
                $PlainPassword = $null
                $temp = $null
                $password = $null
                $key = $null
                Write-Output "Waiting 60 seconds for clearing password from clipboard..., please do NOT terminate the script!"
                Start-Sleep -Seconds 60
                #clear clipboard after 30 seconds
                Write-Verbose "Clearing Clipboard after 60 seconds"
                Add-Type -AssemblyName System.Windows.Forms
                [System.Windows.Forms.Clipboard]::Clear()
                
            }
            #clearing all variables
            Write-Verbose "Clearing variable with plain password"
            $PlainPassword = $null
            $temp = $null
            $password = $null
            $key = $null 
        }

Solltet ihr das Passwort also mal händisch eingeben müssen, könnt ihr das mit diesem Skript direkt erledigen. Wenn euch dieser Artikel oder das Skript gefallen hat, dann drückt auf „helpful“. Lasst mir auch gerne Kommentare da.

Print Friendly, PDF & Email
Was this article helpful?
YesNo
0 0 votes
Article Rating
Abonnieren
Benachrichtige mich bei
guest
3 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Johannes Klumpp
Johannes Klumpp
3 Jahre zuvor

Super Artikel, vielen Dank dafür!!

Dani
Dani
4 Jahre zuvor

Salut
Ich finde deine Doku superbe. danke auch dafür.
Ich versuche mit ein verschlüsseln Password dieses Script auszuführen in ein Programme, wenn ich es im Powershell ausführe geht es ohne Problem.
.\Powershell.exe -noninteractive -executionpolicy unrestricted -file C:\scripts\WLC_Secure.ps1 -Configfile C:\scripts\controller -Volume MasterTest -Minutes 3. Das Problem bei dieses Programme, es wird mit dem SystemUser ausgeführt. Habe es auch mit PsExec64 ausprobiert leider ohne Erfolg.
Meine Frage an dich, was mache ich Falch, oder besser gesagt wie muss ich das im Progamme ausführen.
Lg Dani