Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #----------------------------------------#
- # Update plex auto |
- # This Module is needed |
- # Install-Module -Name Posh-SSH -Verbose |
- #----------------------------------------#
- [CmdLetBinding()]
- Param
- (
- [Parameter(Mandatory=$true,Position=0, HelpMessage='Plex Ip')]
- [string]$ServerIp,
- [Parameter(Mandatory=$true,Position=0, HelpMessage='Ubuntu pw')]
- [string]$pw,
- [Parameter(Mandatory=$true,Position=0, HelpMessage='Ubuntu UserName')]
- [string]$User
- )
- #----------------------#
- # Find Distro you need |
- #----------------------#
- $plex = Invoke-RestMethod -Uri 'https://plex.tv/api/downloads/1.json?_=1514585869146'
- foreach ($item in $plex.computer.Linux.releases)
- {
- if ($item.label -match 'Ubuntu 64-bit')
- {
- $plex_ubuntu = $item
- $plex_ubuntu | Add-Member -MemberType NoteProperty -Name 'Version' -Value $plex.computer.Linux.version
- }
- }
- #-----------------------------#
- # Download and install update |
- #-----------------------------#
- $secpasswd = ConvertTo-SecureString $pw -AsPlainText -Force
- $mycreds = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)
- try
- {
- # Create Session and Stream
- $s = New-SSHSession -ComputerName $ServerIp -AcceptKey -Credential $mycreds -ErrorAction SilentlyContinue
- $secpas = ConvertTo-SecureString $pw -AsPlainText -Force
- $stream = $s.Session.CreateShellStream("PS-SSH", 200, 0, 0, 0, 1000)
- $SSHusersName = Invoke-SSHCommandStream -Command 'whoami' -SSHSession $s
- $Hostname = Invoke-SSHCommandStream -Command 'hostname' -SSHSession $s
- # Check plex Version, if up todate exit, otherwise update plex
- $version = Invoke-SSHStreamExpectSecureAction -ShellStream $stream -Command "sudo dpkg -l plexmediaserver" -ExpectString "[sudo] password for $($SSHusersName):" -SecureAction $secpas
- Start-Sleep 1
- $v = $stream.read()
- $v = $v.split("`r`n") | where {$_ -match 'plexmediaserve'}
- $v = (([regex]::Replace($v,"\s{2,}"," ")).split(" ")[2]).trim()
- if ([regex]::Match($v,$plex_ubuntu.Version).success)
- {
- Write-Host -ForegroundColor Yellow 'Plex is up todate'
- }
- else
- {
- $results = Invoke-SSHStreamExpectSecureAction -ShellStream $stream -Command "sudo wget $($plex_ubuntu.url); sudo dpkg -i $($plex_ubuntu.url.split('/')[-1])" -ExpectString "[sudo] password for $($SSHusersName):" -SecureAction $secpas
- $count = 0
- do
- {
- clear
- $status = $stream.Read()
- Write-Host -ForegroundColor Yellow $status
- Start-Sleep -Milliseconds 500
- $count++
- } until ($status.Contains("$user@$($hostname):~$") -or $count -gt 60)
- Write-Host -ForegroundColor Green "Plex is updatet to $($plex_ubuntu.Version)"
- }
- $s.Disconnect()
- }
- catch
- {
- Write-Host -ForegroundColor Red 'Script failed, to lazy to write prober Exception'
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement