Export PSCredentials To File
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# Usage: # Export-PSCredential [-Credential <PSCredential object>] [-Path <file to export>] # Export-PSCredential [-Credential <username>] [-Path <file to export>] # If Credential is not specififed, user is prompted by Get-Credential cmdlet. # If a username is specified, then Get-Credential will prompt for password. # If the Path is not specififed, it will default to "./credentials.enc.xml". # Output: FileInfo object referring to saved credentials # # Import-PSCredential [-Path <file to import>] # If not specififed, Path is "./credentials.enc.xml". # Output: PSCredential object function Export-PSCredential { param ( $Credential = (Get-Credential), $Path = "credentials.enc.xml" ) # Look at the object type of the $Credential parameter to determine how to handle it switch ( $Credential.GetType().Name ) { # It is a credential, so continue PSCredential { continue } # It is a string, so use that as the username and prompt for the password String { $Credential = Get-Credential -credential $Credential } # In all other caess, throw an error and exit default { Throw "You must specify a credential object to export to disk." } } # Create temporary object to be serialized to disk $export = New-Object PSObject Add-Member -InputObject $export -Name Username -Value $Credential.Username ` -MemberType NoteProperty # Encrypt SecureString password using Data Protection API $EncryptedPassword = $Credential.Password | ConvertFrom-SecureString Add-Member -InputObject $export -Name EncryptedPassword -Value $EncryptedPassword ` -MemberType NoteProperty # Export using the Export-Clixml cmdlet $export | Export-Clixml $Path Write-Host -foregroundcolor Green "Credentials saved to: " -noNewLine # Return FileInfo object referring to saved credentials Get-Item $Path } function Import-PSCredential { param ( $Path = "credentials.enc.xml" ) # Import credential file $import = Import-Clixml $Path # Test for valid import if ( !$import.UserName -or !$import.EncryptedPassword ) { Throw "Input is not a valid ExportedPSCredential object, exiting." } $Username = $import.Username # Decrypt the password and store as a SecureString object for safekeeping $SecurePass = $import.EncryptedPassword | ConvertTo-SecureString # Build the new credential object $Credential = New-Object System.Management.Automation.PSCredential $Username, $SecurePass Write-Output $Credential } |
As stated in this article, you can browse your selection of available deals on smartphones and top brands and explore the cell phone service plans that best suit your needs.
16 thoughts on “Export PSCredentials To File”
Excellent way of describing, and fastidious article to obtain data regarding my presentation subject, which i am going to deliver in school. Maurice Pittinger
Great, thanks for sharing this article post. Cool. Buster Raimann
So you are a man or woman?
Man
Where there is a will, there is a way.
I really like and appreciate your article. Really Cool. Chauncey Vatter
Hello. This article was extremely fascinating, particularly since I was investigating for thoughts on this topic last Sunday. Marlon Person
This Domain Is Good!
Very Good!不错呀!
Thanks for sharing your info. I truly appreciate your efforts and I will be waiting for your further post thanks once again. Augustus Bankos
If some one needs expert view about blogging then i propose him/her to pay a quick visit this web site, Keep up the nice work. Tobias Scarlata
Your way of describing the whole thing in this post is genuinely good, every one can simply be aware of it, Thanks a lot. Tod Sickle
I think you have remarked some very interesting details, appreciate it for the post. Gale Wussow
My coder is trying to convince me to move to .net from PHP. Johnathon Sardin
Everything is very open with a really clear clarification of the challenges. It was really informative. Your website is extremely helpful. Thank you for sharing!
Nice