-
Notifications
You must be signed in to change notification settings - Fork 61
/
ssh-apt-upgrade-ubuntu.ps1
30 lines (30 loc) · 1.21 KB
/
ssh-apt-upgrade-ubuntu.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
# This script performs a software update on a Ubuntu or Debian 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)
$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)
# Run apt=get update
$result = Invoke-SSHStreamExpectSecureAction -ShellStream $stream -Command "apt-get update" -ExpectString "[sudo] password for $($user):" -SecureAction $creds.Password
# Wait 30 seconds for apt-get update to run
Start-Sleep -Seconds 30
$return = $stream.Read()
Write-Host $return
# Run apt-get upgrade
$result = Invoke-SSHStreamExpectSecureAction -ShellStream $stream -Command "apt-get upgrade" -ExpectString "[sudo] password for $($user):" -SecureAction $creds.Password
# Wait 2 minutes for apt-get upgrade to run
Start-Sleep -Seconds 120
$return = $stream.Read()
Write-Host $return
Remove-SSHSession -SessionId 0