PoshCode Logo PowerShell Code Repository

Export-PSCredential by halr9000 3 years ago (modification of post by halr9000 view diff)
View followups from halr9000 | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/474"></script>download | new post

original filename: lib-authentication.ps1

These functions allow one to easily save network credentials to disk in a relatively secure manner. The resulting on-disk credential file can only [1] be decrypted by the same user account which performed the encryption. For more details, see the help files for ConvertFrom-SecureString and ConvertTo-SecureString as well as MSDN pages about Windows Data Protection API.

[1]: So far as I know today. Next week I’m sure a script kiddie will break it.

rev 2: added more comments
rev 3: removed custom type name due to issues on v1
rev 4: re-worked erorr checking to allow for username strings like get-credential
rev 5: fixed something I broke

  1. # Author:       Hal Rottenberg <hal@halr9000.com>
  2. # Url:          http://halr9000.com/article/tag/lib-authentication.ps1
  3. # Purpose:      These functions allow one to easily save network credentials to disk in a relatively
  4. #                       secure manner.  The resulting on-disk credential file can only [1] be decrypted
  5. #                       by the same user account which performed the encryption.  For more details, see
  6. #                       the help files for ConvertFrom-SecureString and ConvertTo-SecureString as well as
  7. #                       MSDN pages about Windows Data Protection API.
  8. #                       [1]: So far as I know today.  Next week I'm sure a script kiddie will break it.
  9. #
  10. # Usage:        Export-PSCredential [-Credential <PSCredential object>] [-Path <file to export>]
  11. #                       Export-PSCredential [-Credential <username>] [-Path <file to export>]
  12. #                       If Credential is not specififed, user is prompted by Get-Credential cmdlet.
  13. #                       If a username is specified, then Get-Credential will prompt for password.
  14. #                       If the Path is not specififed, it will default to "./credentials.enc.xml".
  15. #                       Output: FileInfo object referring to saved credentials
  16. #
  17. #                       Import-PSCredential [-Path <file to import>]
  18. #
  19. #                       If not specififed, Path is "./credentials.enc.xml".
  20. #                       Output: PSCredential object
  21.  
  22. function Export-PSCredential {
  23.         param ( $Credential = (Get-Credential), $Path = "credentials.enc.xml" )
  24.  
  25.         # Look at the object type of the $Credential parameter to determine how to handle it
  26.         switch ( $Credential.GetType().Name ) {
  27.                 # It is a credential, so continue
  28.                 PSCredential            { continue }
  29.                 # It is a string, so use that as the username and prompt for the password
  30.                 String                          { $Credential = Get-Credential -credential $Credential }
  31.                 # In all other caess, throw an error and exit
  32.                 default                         { Throw "You must specify a credential object to export to disk." }
  33.         }
  34.        
  35.         # Create temporary object to be serialized to disk
  36.         $export = "" | Select-Object Username, EncryptedPassword
  37.        
  38.         # Give object a type name which can be identified later
  39.         $export.PSObject.TypeNames.Insert(0,’ExportedPSCredential’)
  40.        
  41.         $export.Username = $Credential.Username
  42.  
  43.         # Encrypt SecureString password using Data Protection API
  44.         # Only the current user account can decrypt this cipher
  45.         $export.EncryptedPassword = $Credential.Password | ConvertFrom-SecureString
  46.  
  47.         # Export using the Export-Clixml cmdlet
  48.         $export | Export-Clixml $Path
  49.         Write-Host -foregroundcolor Green "Credentials saved to: " -noNewLine
  50.  
  51.         # Return FileInfo object referring to saved credentials
  52.         Get-Item $Path
  53. }
  54.  
  55. function Import-PSCredential {
  56.         param ( $Path = "credentials.enc.xml" )
  57.  
  58.         # Import credential file
  59.         $import = Import-Clixml $Path
  60.        
  61.         # Test for valid import
  62.         if ( !$import.UserName -or !$import.EncryptedPassword ) {
  63.                 Throw "Input is not a valid ExportedPSCredential object, exiting."
  64.         }
  65.         $Username = $import.Username
  66.        
  67.         # Decrypt the password and store as a SecureString object for safekeeping
  68.         $SecurePass = $import.EncryptedPassword | ConvertTo-SecureString
  69.        
  70.         # Build the new credential object
  71.         $Credential = New-Object System.Management.Automation.PSCredential $Username, $SecurePass
  72.         Write-Output $Credential
  73. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me