PoshCode Logo PowerShell Code Repository

Export-PSCredential by halr9000 5 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/471"></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

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

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