-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPasswordAssist.ps1
59 lines (52 loc) · 1.88 KB
/
PasswordAssist.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
param (
[Parameter(Mandatory = $false)][string]$ConfigFile,
[Parameter(Mandatory = $false)][string]$ServerPublicKeyFile,
[ValidateSet("EncryptPassword", "SetMysqlPassword", "DownloadPublicKey")]
[string]$Action
)
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. ($here | Join-Path -ChildPath 'scripts' | Join-Path -ChildPath 'global-variables.ps1')
. $Global:CommonUtil
. $Global:ClientUtil
"importing $($Global:ClientUtil)" | Write-Verbose
"importing $($Global:CommonUtil)" | Write-Verbose
while ((-not $ConfigFile) -or (-not (Test-Path -Path $ConfigFile))) {
$ConfigFile = Read-Host -Prompt "Please enter the path of configuration file:"
}
$configuration = Get-Configuration -ConfigFile $ConfigFile
if (-not $configuration) {
return
}
Copy-PsScriptToServer -ConfigFile $ConfigFile
switch ($Action) {
"EncryptPassword" {
if (-not $ServerPublicKeyFile) {
$ServerPublicKeyFile = Get-ServerPublicKeyFile
}
Protect-PasswordByOpenSSLPublicKey -ServerPublicKeyFile $ServerPublicKeyFile
break
}
"SetMysqlPassword" {
if (-not $ServerPublicKeyFile) {
$ServerPublicKeyFile = Get-ServerPublicKeyFile
}
$s = Protect-PasswordByOpenSSLPublicKey -ServerPublicKeyFile $ServerPublicKeyFile
$Global:configuration.MysqlPassword = $s
$Global:configuration.PSObject.Properties.Remove('OsConfig')
$Global:configuration | ConvertTo-Json -Depth 10 | Out-File $ConfigFile -Encoding utf8
break
}
"DownloadPublicKey" {
$r = Invoke-ServerRunningPs1 -action $Action
$r | Write-Verbose
$r = $r | Receive-LinesFromServer
$sshInvoker = Get-SshInvoker
$f = Get-ServerPublicKeyFile -NotResolve
$sshInvoker.ScpFrom($r, $f, $false)
$sshInvoker.invoke("rm $r")
break
}
Default {
"Unimplement action."
}
}