forked from jrussellfreelance/powershell-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh-start-web.ps1
26 lines (26 loc) · 1.24 KB
/
ssh-start-web.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
# This script starts the nginx and php-fpm services on the specified server
param(
[Parameter(Mandatory=$true)]$server,
[Parameter(Mandatory=$true)]$username,
[Parameter(Mandatory=$true)]$password
)
# Create credential object based on parameters
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
# Create SSH session
$session = New-SSHSession -ComputerName $server -Credential $creds
# Convert entered password to secure string
$secPass = ConvertTo-SecureString $creds.Password -AsPlainText -Force
# Start Shell Stream
$stream = $session.Session.CreateShellStream("PS-SSH", 0, 0, 0, 0, 1000)
# Start nginx and php-fpm
$result = Invoke-SSHStreamExpectSecureAction -ShellStream $stream -Command "sudo systemctl atart php7.0-fpm" -ExpectString "[sudo] password for $($user):" -SecureAction $creds.Password
Start-Sleep -Seconds 1
$return = $stream.Read()
Write-Host $return
$result = Invoke-SSHStreamExpectSecureAction -ShellStream $stream -Command "sudo systemctl atart nginx" -ExpectString "[sudo] password for $($user):" -SecureAction $creds.Password
Start-Sleep -Seconds 1
$return = $stream.Read()
Write-Host $return
Remove-SSHSession -SessionId 0
# Finished