Skip to content

Commit c160490

Browse files
Added import/export cred
1 parent f43718d commit c160490

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

Export-PSCredential.ps1

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
function Export-PSCredential {
2+
<#
3+
.SYNOPSIS
4+
Export credentials to a file
5+
6+
.DESCRIPTION
7+
Export credentials to a file
8+
For use with Import-PSCredential
9+
A credential can only be decrypted by the user who encryped it, on the computer where the command was invoked.
10+
11+
.PARAMETER Credential
12+
Credential to export
13+
14+
.PARAMETER Path
15+
File to export to. Parent folder must exist
16+
17+
.PARAMETER Passthru
18+
Return FileInfo object for the credential file
19+
20+
.EXAMPLE
21+
22+
#Creates a credential, saves it to disk
23+
$Credential = Get-Credential
24+
Export-PSCredential -path C:\File.xml -credential $Credential
25+
26+
#Later on, import the credential!
27+
$ImportedCred = Import-PSCredential -path C:\File.xml
28+
29+
.NOTES
30+
Author: Hal Rottenberg <[email protected]>, butchered by ramblingcookiemonster
31+
Purpose: These functions allow one to easily save network credentials to disk in a relatively
32+
secure manner. The resulting on-disk credential file can only [1] be decrypted
33+
by the same user account which performed the encryption. For more details, see
34+
the help files for ConvertFrom-SecureString and ConvertTo-SecureString as well as
35+
MSDN pages about Windows Data Protection API.
36+
[1]: So far as I know today. Next week I'm sure a script kiddie will break it.
37+
38+
.FUNCTIONALITY
39+
General Command
40+
#>
41+
[cmdletbinding()]
42+
param (
43+
[parameter(Mandatory=$true)]
44+
[pscredential]$Credential = (Get-Credential),
45+
46+
[parameter()]
47+
[Alias("FullName")]
48+
[validatescript({
49+
Test-Path -Path (Split-Path -Path $_ -Parent)
50+
})]
51+
[string]$Path = "credentials.$env:COMPUTERNAME.xml",
52+
53+
[switch]$Passthru
54+
)
55+
56+
# Create temporary object to be serialized to disk
57+
$export = New-Object -TypeName PSObject -Property @{
58+
UserName = $Credential.Username
59+
EncryptedPassword = $Credential.Password | ConvertFrom-SecureString
60+
}
61+
62+
# Export using the Export-Clixml cmdlet
63+
Try
64+
{
65+
$export | Export-Clixml -Path $Path -ErrorAction Stop
66+
Write-Verbose "Saved credentials for $($export.Username) to $Path"
67+
68+
if($Passthru)
69+
{
70+
# Return FileInfo object referring to saved credentials
71+
Get-Item $Path -ErrorAction Stop
72+
}
73+
}
74+
Catch
75+
{
76+
Write-Error "Error saving credentials to '$Path': $_"
77+
}
78+
}

Import-PSCredential.ps1

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function Import-PSCredential {
2+
<#
3+
.SYNOPSIS
4+
Import credentials from a file
5+
6+
.DESCRIPTION
7+
Export credentials to a file
8+
For use with Import-PSCredential
9+
A credential can only be decrypted by the user who encryped it, on the computer where the command was invoked.
10+
11+
.PARAMETER Path
12+
Path to credential file
13+
14+
.PARAMETER GlobalVariable
15+
If specified, store the imported credential in a global variable with this name
16+
17+
.EXAMPLE
18+
19+
#Creates a credential, saves it to disk
20+
$Credential = Get-Credential
21+
Export-PSCredential -path C:\File.xml -credential $Credential
22+
23+
#Later on, import the credential!
24+
$ImportedCred = Import-PSCredential -path C:\File.xml
25+
26+
.NOTES
27+
Author: Hal Rottenberg <[email protected]>, butchered by ramblingcookimonster
28+
Purpose: These functions allow one to easily save network credentials to disk in a relatively
29+
secure manner. The resulting on-disk credential file can only [1] be decrypted
30+
by the same user account which performed the encryption. For more details, see
31+
the help files for ConvertFrom-SecureString and ConvertTo-SecureString as well as
32+
MSDN pages about Windows Data Protection API.
33+
[1]: So far as I know today. Next week I'm sure a script kiddie will break it.
34+
.FUNCTIONALITY
35+
General Command
36+
37+
#>
38+
[cmdletbinding()]
39+
param (
40+
[Alias("FullName")]
41+
[validatescript({
42+
Test-Path -Path $_
43+
})]
44+
[string]$Path = "credentials.$env:computername.xml",
45+
46+
[string]$GlobalVariable
47+
)
48+
49+
# Import credential file
50+
$import = Import-Clixml -Path $Path -ErrorAction Stop
51+
52+
# Test for valid import
53+
if ( -not $import.UserName -or -not $import.EncryptedPassword ) {
54+
Throw "Input is not a valid ExportedPSCredential object."
55+
}
56+
57+
# Build the new credential object
58+
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $import.Username, $($import.EncryptedPassword | ConvertTo-SecureString)
59+
60+
if ($OutVariable)
61+
{
62+
New-Variable -Name $GlobalVariable -scope Global -value $Credential -Force
63+
}
64+
else
65+
{
66+
$Credential
67+
}
68+
}

0 commit comments

Comments
 (0)