-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-sjPassword-Function.ps1
185 lines (167 loc) · 7.37 KB
/
Set-sjPassword-Function.ps1
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
function Set-sjPassword {
<#
.NOTES
PowerShell function written to store a username and password in the registry
or in a file for use with scripts that need credentials
Written by Steven Judd, 2014/06/11
Updated by Steven Judd on 2014/06/11
Updated by Steven Judd on 2015/04/13
Updated by Steven Judd on 2015/08/17 to add the option to store the password
in a file instead of the registry
Updated by Steven Judd on 2019/10/26 to remove the File parameter, tighten up
the code, make the Filename mandatory
Updated by Steven Judd on 2019/10/27 to add a Clixml switch parameter to allow
taking the credentials specified and export a PSCredential object into the
FileName specified.
Version 20191027.1
Feature requests:
-
.SYNOPSIS
Gets username and password and stores it in either the registry or a text
file as secure text
.DESCRIPTION
This is a very basic function that prompts for the username and password by
using the Get-Credential command. It takes the input, converts the password
to secure string, and either creates a Registry Key location based on the
username to store the username, password, and the date and time of when the
password was created, or it creates a specified file and stores the password
in that file.
The purpose for creating a file is that the registry keys for HKCU are not
available if the user is not logged on. Thus if you want to create an automation
where the username and password are retrieved without the user being logged on
you will need to use the Filename parameter to create a file with the password.
The password that is stored can only be accessed by the person that created
it on the machine where they created it.
.LINK
http://notspecified
.PARAMETER UserName
This parameter will specify the username to prompt for the credentials. If
no username is specified, the script will use the current user.
.PARAMETER FileName
This parameter will specify the filename into which to save the encrypted
credentials. This value is mandatory if the parameter is specified.
.PARAMETER Clixml
This switch parameter will export the credentials gathered into the filename
specified in the FileName parameter using Export-Clixml. It will store the
username and the encrypted password in the file.
.EXAMPLE
Set-sjPassword
This command will prompt for the password for the current user and store the
resulting information under the key HKCU:\Software\PasswordCache in
the registry.
.EXAMPLE
Set-sjPassword -UserName domain01\user01
This command will prompt for the password for the user "user01" in the
domain "domain01" and store the resulting information under the key
HKCU:\Software\PasswordCache\domain01\user01 in the registry.
.EXAMPLE
Set-sjPassword -UserName test -Filename testPwd.txt -Verbose
This command will prompt for the password for the user "test" and put the
encrypted password into a file named "testPwd.txt" in the current directory.
#>
[CmdletBinding(DefaultParameterSetName = "Registry")]
param (
[Parameter(Mandatory = $false,
Position = 0,
ParameterSetName = "Registry")]
[Parameter(Mandatory = $false,
Position = 0,
ParameterSetName = "File")]
[string]$UserName = "$env:userdomain\$env:username", #current user with domain
[Parameter(Mandatory = $true,
Position = 1,
ParameterSetName = "File",
HelpMessage = "Enter a valid path and filename for the password file")]
[string]$Filename,
[Parameter(Mandatory = $false,
Position = 2,
ParameterSetName = "File")]
[switch]$Clixml
)
Write-Verbose "Get Username and password"
$Cred = Get-Credential -UserName $UserName -Message "Enter the username and password"
if (!$Cred) {
#if Cancel was clicked on the credential prompt
Write-Warning "Script cancelled"
Return
}
if ($Cred.Password.Length -gt 0) {
$Password = ConvertFrom-SecureString -SecureString $Cred.Password
}
else {
#password is blank
Write-Warning "Password is blank. This is insecure and will most likely be an issue."
}
$CredUserName = $Cred.UserName
Write-Verbose "UserName -- $CredUserName"
[datetime]$currDateTime = Get-Date
Write-Verbose "DateTime -- $currDateTime"
if ($Filename) {
Write-Verbose "FilePath -- $Filename"
}
else {
$RegPath = "HKCU:\Software\sjPasswordCache\$CredUserName"
Write-Verbose "RegPath -- $RegPath"
}
if ($Filename) {
Write-Verbose "Password file creation and/or update"
if (-not(Test-Path -Path $Filename)) {
try {
Write-Verbose "Creating password file -- $Filename"
New-Item -Path $Filename -ItemType File -Force | Write-Verbose
}
catch {
throw $_
}
} #end if (-not(Test-Path -Path $Filename))
try {
Write-Verbose "Writing password to file -- $Filename"
if ($Clixml) {
$Cred | Export-Clixml -Path $Filename -Force
}
else {
Out-File -FilePath $Filename -InputObject $Password -Force
}
Write-Verbose "Contents of $Filename -- $(Get-Content $Filename)"
}
catch {
throw $_
}
} #end if ($Filename)
else {
Write-Verbose "Password registry creation and/or update"
if (Test-Path -Path $RegPath) {
try {
Write-Verbose "Set registry values"
Set-ItemProperty -Path $RegPath -Name PSUserName -Value $UserName
Get-ItemProperty -Path $RegPath -Name PSUserName | Write-Verbose
Set-ItemProperty -Path $RegPath -Name PSPasswordString -Value $Password
Get-ItemProperty -Path $RegPath -Name PSPasswordString | Write-Verbose
Set-ItemProperty -Path $RegPath -Name PSPasswordDate -Value $currDateTime
Get-ItemProperty -Path $RegPath -Name PSPasswordDate | Write-Verbose
}
catch {
throw $_
}
} #end if
else {
try {
Write-Verbose "Create registry values"
New-Item -Path $RegPath -Force | Write-Verbose
New-ItemProperty -Path $RegPath -Name PSUserName -Value $UserName | Write-Verbose
New-ItemProperty -Path $RegPath -Name PSPasswordDate -Value $currDateTime | Write-Verbose
New-ItemProperty -Path $RegPath -Name PSPasswordString -Value $Password | Write-Verbose
}
catch {
throw $_
}
}
}
#endregion
} #end function
#Test commands
# Set-sjPassword
# Set-sjPassword -UserName test -Verbose
# Set-sjPassword -UserName test -Filename (Join-Path -Path $env:temp -ChildPath "testPwd.txt") -Verbose
# Set-sjPassword -UserName test -Filename (Join-Path -Path $env:temp -ChildPath "testPwd.txt") -Clixml -Verbose
# Set-sjPassword -Filename -Verbose