-
Notifications
You must be signed in to change notification settings - Fork 13
/
Functions-PSStoredCredentials.ps1
174 lines (123 loc) · 4.79 KB
/
Functions-PSStoredCredentials.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
<#
.SYNOPSIS
Functions-PSStoredCredentials - PowerShell functions to manage stored credentials for re-use
.DESCRIPTION
This script adds two functions that can be used to manage stored credentials
on your admin workstation.
.EXAMPLE
. .\Functions-PSStoredCredentials.ps1
.LINK
https://practical365.com/blog/saving-credentials-for-office-365-powershell-scripts-and-scheduled-tasks
.NOTES
Written by: Paul Cunningham
Find me on:
* My Blog: https://paulcunningham.me
* Twitter: https://twitter.com/paulcunningham
* LinkedIn: https://au.linkedin.com/in/cunninghamp/
* Github: https://github.com/cunninghamp
#>
Function New-StoredCredential {
<#
.SYNOPSIS
New-StoredCredential - Create a new stored credential
.DESCRIPTION
This function will save a new stored credential to a .cred file.
.EXAMPLE
New-StoredCredential
.LINK
https://practical365.com/saving-credentials-for-office-365-powershell-scripts-and-scheduled-tasks
.NOTES
Written by: Paul Cunningham
Find me on:
* My Blog: http://paulcunningham.me
* Twitter: https://twitter.com/paulcunningham
* LinkedIn: http://au.linkedin.com/in/cunninghamp/
* Github: https://github.com/cunninghamp
For more Office 365 tips, tricks and news
check out Practical 365.
* Website: https://practical365.com
* Twitter: https://twitter.com/practical365
#>
if (!(Test-Path Variable:\KeyPath)) {
Write-Warning "The `$KeyPath variable has not been set. Consider adding `$KeyPath to your PowerShell profile to avoid this prompt."
$path = Read-Host -Prompt "Enter a path for stored credentials"
Set-Variable -Name KeyPath -Scope Global -Value $path
if (!(Test-Path $KeyPath)) {
try {
New-Item -ItemType Directory -Path $KeyPath -ErrorAction STOP | Out-Null
}
catch {
throw $_.Exception.Message
}
}
}
$Credential = Get-Credential -Message "Enter a user name and password"
$Credential.Password | ConvertFrom-SecureString | Out-File "$($KeyPath)\$($Credential.Username).cred" -Force
# Return a PSCredential object (with no password) so the caller knows what credential username was entered for future recalls
New-Object -TypeName System.Management.Automation.PSCredential($Credential.Username,(new-object System.Security.SecureString))
}
Function Get-StoredCredential {
<#
.SYNOPSIS
Get-StoredCredential - Retrieve or list stored credentials
.DESCRIPTION
This function can be used to list available credentials on
the computer, or to retrieve a credential for use in a script
or command.
.PARAMETER UserName
Get the stored credential for the username
.PARAMETER List
List the stored credentials on the computer
.EXAMPLE
Get-StoredCredential -List
.EXAMPLE
$credential = Get-StoredCredential -UserName [email protected]
.EXAMPLE
Get-StoredCredential -List
.LINK
https://practical365.com/saving-credentials-for-office-365-powershell-scripts-and-scheduled-tasks
.NOTES
Written by: Paul Cunningham
Find me on:
* My Blog: http://paulcunningham.me
* Twitter: https://twitter.com/paulcunningham
* LinkedIn: http://au.linkedin.com/in/cunninghamp/
* Github: https://github.com/cunninghamp
For more Office 365 tips, tricks and news
check out Practical 365.
* Website: https://practical365.com
* Twitter: https://twitter.com/practical365
#>
param(
[Parameter(Mandatory=$false, ParameterSetName="Get")]
[string]$UserName,
[Parameter(Mandatory=$false, ParameterSetName="List")]
[switch]$List
)
if (!(Test-Path Variable:\KeyPath)) {
Write-Warning "The `$KeyPath variable has not been set. Consider adding `$KeyPath to your PowerShell profile to avoid this prompt."
$path = Read-Host -Prompt "Enter a path for stored credentials"
Set-Variable -Name KeyPath -Scope Global -Value $path
}
if ($List) {
try {
$CredentialList = @(Get-ChildItem -Path $keypath -Filter *.cred -ErrorAction STOP)
foreach ($Cred in $CredentialList) {
Write-Host "Username: $($Cred.BaseName)"
}
}
catch {
Write-Warning $_.Exception.Message
}
}
if ($UserName) {
if (Test-Path "$($KeyPath)\$($Username).cred") {
$PwdSecureString = Get-Content "$($KeyPath)\$($Username).cred" | ConvertTo-SecureString
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $PwdSecureString
}
else {
throw "Unable to locate a credential for $($Username)"
}
return $Credential
}
}