-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReadSecurePW.ps1
More file actions
69 lines (58 loc) · 2 KB
/
ReadSecurePW.ps1
File metadata and controls
69 lines (58 loc) · 2 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
61
62
63
64
65
66
67
68
69
<#
.SYNOPSIS
Save Encrypted Passwords to Registry for PowerShell
.DESCRIPTION
Read user text input, convert to secure string, and save to HKCU for usage as credential across many PowerShell scripts.
.NOTES
File Name : ReadSecurePW.ps1
Author : Jeff Jones - @spjeff
Version : 0.02
Last Modified : 01-29-2017
.LINK
http://www.github.com/spjeff/spadmin/ReadSecurePW.ps1
#>
param (
[Alias("c")]
[switch]$clearSavedPW
)
Function GetSecurePassword($user) {
# Registry HKCU folder
$path = "HKCU:\Software\AdminScript"
if (!(Test-Path $path)) {
mkdir $path | Out-Null
}
$name = $user
# Do we need to clear old paswords?
if ($clearSavedPW) {
Remove-ItemProperty -Path $path -Name $name -Confirm:$false -ErrorAction SilentlyContinue
Write-Host "Deleted password OK for $name" -Fore Yellow
Exit
}
# Do we have registry HKCU saved password?
$hash = (Get-ItemProperty -Path $path -Name $name -ErrorAction SilentlyContinue)."$name"
# Prompt for input
if (!$hash) {
$sec = Read-Host "Enter Password for $name" -AsSecureString
if (!$sec) {
Write-Error "Exit - No password given"
Exit
}
$hash = $sec | ConvertFrom-SecureString
# Prompt to save to HKCU
$save = Read-Host "Save to HKCU registry (secure hash) [Y/N]?"
if ($save -like "Y*") {
Set-ItemProperty -Path $path -Name $name -Value $hash -Force
Write-Host "Saved password OK for $name" -Fore Yellow
}
}
# Return
return $hash
}
# Example usage for SharePoint Online (Office 365)
Import-Module Microsoft.Online.SharePoint.PowerShell -WarningAction SilentlyContinue
$admin = "admin@tenant.onmicrosoft.com"
$pass = GetSecurePassword $admin
$secpw = ConvertTo-SecureString -String $pass -AsPlainText -Force
$c = New-Object System.Management.Automation.PSCredential ($admin, $secpw)
Connect-SPOService -URL "https://tenant-admin.sharepoint.com" -Credential $c
Get-SPOSite