Save-Credential.ps1

<#
.Synopsis
   Store credential object to disk.
.DESCRIPTION
   Store credential object to disk to be used with automation.
.NOTES
Created by: Jason Wasser @wasserja
Modified: 4/20/2017 11:39:09 AM
.EXAMPLE
PS C:\> Save-Credential
.EXAMPLE
PS C:\> Save-Credential -Username jsmith -EncryptionKeyPath c:\Keys\jsmith.key
#>

function Save-Credential {

    [cmdletbinding()]
    param (
        $CredentialFilePath = 'C:\Scripts\Logon.xml',
        [string]$UserName = $env:USERNAME,
        [securestring]$Password,
        $Message = 'Enter credentials',
        $EncryptionKeyPath
        )

    begin {
        $ErrorActionPreference = 'Stop'
        Write-Verbose "ErrorActionPreference: $ErrorActionPreference"
        }
    process {
        # We'll do code later for username and password
        if ($Password) {
            Write-Verbose -Message 'Password has been supplied'
        
            try {
                $Credential = New-Object -type System.Management.Automation.PSCredential($UserName,$Password)
                }
            catch {
                Write-Error $Error[0].Exception.Message
                return
                }
        
            }
        else {
            Write-Verbose -Message 'No Password has been supplied. Prompting for Credential.'
            try {
                $Credential = Get-Credential -UserName $UserName -Message $Message
                }
            catch {
                Write-Error $Error[0].Exception.Message
                return
                }
        
            }

        if ($EncryptionKeyPath) {
            try {
                Write-Verbose -Message 'Encryption Key Path Specified.'
                $EncryptionKey = [byte[]](Get-Content $EncryptionKeyPath)

                Write-Verbose -Message 'Building exportable object using encryption key.'
                $ExportCredential = New-Object psobject -Property @{
                    UserName = $Credential.UserName
                    Password = ConvertFrom-SecureString -SecureString $Credential.Password -Key $EncryptionKey
                    }

                Write-Verbose -Message "Exporting credential to $CredentialFilePath with encryption key."
                $ExportCredential | Export-Clixml -Path $CredentialFilePath -Force
                }
            catch {
                Write-Error $Error[0].Exception.Message
                return
                }
        
            }
        else {
            try {
                if ($Credential) {
                    Write-Verbose -Message "Exporting credential to $CredentialFilePath"
                    $Credential | Export-Clixml -Path $CredentialFilePath -Force
                    }
                else {
                    Write-Verbose 'No Credential object created.'
                    }
                }
            catch {
                Write-Error $Error[0].Exception.Message
                return
                }
            }
        }
    end {}
    
}