forked from jrussellfreelance/powershell-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh-new-db-mysql.ps1
More file actions
44 lines (44 loc) · 1.27 KB
/
Copy pathssh-new-db-mysql.ps1
File metadata and controls
44 lines (44 loc) · 1.27 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
# This script creates a new MySQL database on the Linux host
# Grab server IP
Do {
$server = Read-Host "Please enter the IP of the Linux server"
}
While ($server -eq "")
Do {
$NEWDB = Read-Host "Database Name"
}
While ($NEWDB -eq "")
Do {
$NEWUSER = Read-Host "Database User"
}
While ($NEWUSER -eq "")
Do {
$NEWPWD = Read-Host "Database Password"
}
While ($NEWPWD -eq "")
Do {
$ROOTPWD = Read-Host "MySQL Root Password"
}
While ($ROOTPWD -eq "")
# Grab credentials
$creds = Get-Credential
$user = $creds.UserName
# Create SSH session
$session = New-SSHSession -ComputerName $server -Credential $creds
# Start Shell Stream
$stream = $session.Session.CreateShellStream("PS-SSH", 0, 0, 0, 0, 1000)
# Create database
$result = Invoke-SSHStreamExpectSecureAction -ShellStream $stream -Command @"
sudo mysql -uroot -p$ROOTPWD <<EOF
CREATE DATABASE $NEWDB;
CREATE USER $NEWUSER@localhost IDENTIFIED BY "$NEWPWD";
GRANT ALL PRIVILEGES ON $NEWDB.* TO $NEWUSER@localhost IDENTIFIED BY '$NEWPWD';
FLUSH PRIVILEGES;
exit
EOF
"@ -ExpectString "[sudo] password for $($user):" -SecureAction $creds.Password
Start-Sleep -Seconds 1
$return = $stream.Read()
Write-Host $return
New-BurntToastNotification -Text "The MySQL database has been created" -AppLogo $null -Silent
Remove-SSHSession -SessionId 0