Public/ConvertFrom-EncryptedString.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
function ConvertFrom-EncryptedString {
    <#
    .SYNOPSIS
    Decrypts a string to a secure string to use it for credentials on the current system

    .DESCRIPTION
    Many cmdlets for handling credentials rely on a SecureString. This function decrypts the sensitive data and converts it to a SecureString

    .PARAMETER EncryptedString
    Encrypted content

    .PARAMETER Key
    Custom encryption key

    .EXAMPLE
    ConvertFrom-EncryptedString -EncryptedString $SecureString -Key 'key012345678'
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory,ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [string]
        $EncryptedString
        ,
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Key
    )

    $ByteData = [System.Text.Encoding]::ASCII.GetBytes($Key)
    ConvertTo-SecureString -String $EncryptedString -Key $ByteData
}