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/473"></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

  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. #
  12. #                       If Credential is not specififed, user is prompted by Get-Credential cmdlet.
  13. #                       If not specififed, Path is "./credentials.enc.xml".
  14. #                       Output: FileInfo object referring to saved credentials
  15. #
  16. #                       Import-PSCredential [-Path <file to import>]
  17. #
  18. #                       If not specififed, Path is "./credentials.enc.xml".
  19. #                       Output: PSCredential object
  20.  
  21. function Export-PSCredential {
  22.         param ( $Credential = (Get-Credential), $Path = "credentials.enc.xml" )
  23.  
  24.         #
  25.         switch ( $Credential.GetType().Name ) {
  26.                 PSCredential            { continue }
  27.                 String                          { $Credential = Get-Credential -credential $Credential }
  28.                 default                         { Throw "You must specify a credential object to export to disk." }
  29.         }
  30.        
  31.         # Create temporary object to be serialized to disk
  32.         $export = "" | Select-Object Username, EncryptedPassword
  33.        
  34.         # Give object a type name which can be identified later
  35.         $export.PSObject.TypeNames.Insert(0,’ExportedPSCredential’)
  36.        
  37.         $export.Username = $Credential.Username
  38.  
  39.         # Encrypt SecureString password using Data Protection API
  40.         # Only the current user account can decrypt this cipher
  41.         $export.EncryptedPassword = $Credential.Password | ConvertFrom-SecureString
  42.  
  43.         # Export using the Export-Clixml cmdlet
  44.         $export | Export-Clixml $Path
  45.         Write-Host -foregroundcolor Green "Credentials saved to: " -noNewLine
  46.  
  47.         # Return FileInfo object referring to saved credentials
  48.         Get-Item $Path
  49. }
  50.  
  51. function Import-PSCredential {
  52.         param ( $Path = "credentials.enc.xml" )
  53.  
  54.         # Import credential file
  55.         $import = Import-Clixml $Path
  56.        
  57.         # Test for valid import
  58.         if ( $import.PSObject.TypeNames -notcontains 'Deserialized.ExportedPSCredential' ) {
  59.                 Throw "Input is not a valid ExportedPSCredential object, exiting."
  60.         }
  61.         $Username = $import.Username
  62.        
  63.         # Decrypt the password and store as a SecureString object for safekeeping
  64.         $SecurePass = $import.EncryptedPassword | ConvertTo-SecureString
  65.        
  66.         # Build the new credential object
  67.         $Credential = New-Object System.Management.Automation.PSCredential $Username, $SecurePass
  68.         Write-Output $Credential
  69. }

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