-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate-EncryptedPasswordFile.ps1
More file actions
60 lines (51 loc) · 2.16 KB
/
Create-EncryptedPasswordFile.ps1
File metadata and controls
60 lines (51 loc) · 2.16 KB
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
<#
.NOTES
===========================================================================
Created with: PowerShell ISE (Win10 18363)
Revision: 2020.02.19.1800
Last Modified: 19 February 2020
Created by: Jay Harper (github.com/thecatdidit/powershellusefulscripts)
Organizaiton: Happy Days Are Here Again
Filename: Create-EncryptedPasswordFile.ps1
===========================================================================
.CHANGELOG
[2020.02.19.1800]
Original script creation
.SYNOPSIS
This script enables secure storage of user credentials in a single,
PSAutomation COM Object so that this variable can be passed when needing
credentials for functions on remote systems.
.DESCRIPTION
The script requests a manually submitted set of user credentials. The password is
encrypted as SecureString, then paired with a 256-bit AES encryption key.
.EXAMPLE
PS C:\> Create-EncryptedPasswordFile.ps1 -FilePath C:\Temp -FileName SomeFile
.INPUTS
-FilePath (MANDATORY)
Configures the location where both the Secure String password file and AES key
are stored.
-FileName (MANDATORY)
Configures the name of the password file and key file
.OUTPUTS
PSCredential System.Object
#>
Function Create-EncryptedPasswordFile($FilePath, $FileName) {
if (Test-Path $FilePath) {
##
#Confirm full proper path with ending backslash
##
if (!($FilePath.EndsWith("\"))) {
$FilePath = $FilePath.Insert($FilePath.Length, "\")
}
##
#Associate encryption key with password file
##
$Key = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | out-file $FilePath\$FileName.key -Force
$YourCreds = Get-Credential
(Get-Credential -Credential $YourCreds).Password | ConvertFrom-SecureString -key (Get-Content $FilePath\$fileName.key) | set-content "$FilePath\$FileName.txt" -Force
$CredsSecure = New-Object System.Management.Automation.PSCredential($YourCreds.UserName, $YourCreds.Password)
Return $CredsSecure
}
}