Jump to content

PowerShell script to update server when running as a service


Quicky

Recommended Posts

Hi all,

 

I've been using emby as a service for while now, and having gone through quite a few updates of the server, have always found it a bit long-winded to complete the update steps, thanks to the emby installer automatically starting the desktop app version of the server rather than the service.

 

I've finally got round to writing a quick PowerShell script that automates it a bit more: stop the current service, download the server installer, run the installer, kill the emby app and finally start the service again. I've posted it here in case anyone's got a similar workflow to me for updating emby, and is only marginally lazier than I am:

# Installer download path
$downloadPath = "$env:USERPROFILE\Downloads"

# Stop the emby service
$service = 'Media Browser'
if ((get-service -DisplayName $service).Status -eq "started")
{
    Stop-Service -DisplayName $service
}

# Download installer
$filename = 'setup.exe'
$source = "http://mb3admin.com/downloads/release/server/$filename"
$destination = $downloadPath + $fileName
Invoke-WebRequest $source -OutFile $destination 

# Run installer
& $destination | Out-Null

# Kill the emby app that gets started.
$emby = Get-Process -Name MediaBrowser.ServerApplication -ErrorAction SilentlyContinue
while (!$emby) 
{
    # Check the app is running. Wait until it starts
    Sleep 5
    $emby = Get-Process -Name MediaBrowser.ServerApplication -ErrorAction SilentlyContinue
}
Stop-Process $emby -Force
Sleep 5

# Delete downloaded installer
Remove-Item $destination

#Restart the emby service
Start-Service -DisplayName $service
Edited by Quicky
  • Like 6
Link to comment
Share on other sites

FrostByte

Thanks, will give it a whirl

 

Edit: I'll have to change a few things because of some errors I got.  It looks like you are still using the old service name, etc and I need to changed to beta download.  I wonder if it can get the level automatically from the system.xml.  Anyway, very cool

Edit 2: works like a champ now :)  Thanks again

Edited by FrostByte
Link to comment
Share on other sites

MSattler

Thanks, will give it a whirl

 

Edit: I'll have to change a few things because of some errors I got.  It looks like you are still using the old service name, etc and I need to changed to beta download.  I wonder if it can get the level automatically from the system.xml.  Anyway, very cool

Edit 2: works like a champ now  :)  Thanks again

Post your changes =)

Link to comment
Share on other sites

Thanks for this. I've been looking for a better way to update the service. 

 

I like to use a different account for the service so I added a couple lines just before it starts the service. 

These change the Recovery, and Log On settings for the service:

sc.exe failure Emby reset= 86400 actions= restart/30000
sc.exe config Emby obj= "username" password=password start= auto
 
Thanks again!
  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...
hatharry

Updated to support media browser service, emby service and fresh installs.

#Test for admin permissions
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   
#"No Administrative rights, it will display a popup window asking user for Admin rights"

$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments

break
}

$ServiceDisplayName = 'Media Browser', 'Emby Server'
$ServiceName = 'MediaBrowser', 'Emby'
$ProcessName = 'MediaBrowser.ServerApplication'
$InstallFolder = 'MediaBrowser-Server', 'Emby-Server'

# Stop the emby service
"Stopping service"
foreach ($string in $ServiceDisplayName)
{
    if ((get-service -DisplayName $string -ErrorAction SilentlyContinue).status -eq 'Running')
    {
        Stop-Service -DisplayName $string
    }
}
if (Get-Process -Name $ProcessName -ErrorAction SilentlyContinue)
{
    Stop-Process -Name $ProcessName -Force
    Wait-Process -Name $ProcessName
}

# Download installer
"Downloading installer"
$SUL = "release"
foreach ($string in $InstallFolder)
{
    if (Test-Path -Path $env:USERPROFILE\AppData\Roaming\$string\config\System.xml)
    {
        $xml = [xml](get-content $env:USERPROFILE\AppData\Roaming\$string\config\System.xml)
        $SUL = $xml.ServerConfiguration.SystemUpdateLevel.ToLower()
    }
}
$downloadPath = "$env:USERPROFILE\Downloads\"
$filename = 'setup.exe'
$source = "http://mb3admin.com/downloads/$SUL/server/$filename"
$destination = $downloadPath + $fileName
Invoke-WebRequest $source -OutFile $destination
Unblock-File $destination

# Run installer
"Running installer"
Start-Process $destination
"Waiting for emby"
if (get-Process -Name $ProcessName -ErrorAction SilentlyContinue)
{
    $process = $TRUE
}
while (!$process)
{
    if (get-Process -Name $ProcessName -ErrorAction SilentlyContinue)
    {
        $process = $TRUE
    }
}

# Kill the emby app that gets started.
"Finding emby service"
foreach ($string in $ServiceDisplayName)
{
    if (Get-Service -DisplayName $string -ErrorAction SilentlyContinue)
    {
        $emby = $TRUE
    }
}
while (!$emby)
{
    # Check the app is running. Wait until it starts
    foreach ($string in $ServiceDisplayName)
    {
        if (Get-Service -DisplayName $string -ErrorAction SilentlyContinue)
        {
            $emby = $TRUE
        }
    }
}
"Closing emby"
if (Get-Process -Name $ProcessName -ErrorAction SilentlyContinue)
{
    Stop-Process -Name $ProcessName -Force
    Wait-Process -Name $ProcessName
}

# Delete downloaded installer
"Deleting installer"
if (Test-Path -Path $destination)
{
    Remove-Item $destination
}

#Restart the emby service
"Setting service to auto startup"
foreach ($string in $ServiceName)
{
    Set-Service -Name $string -StartupType Automatic -ErrorAction SilentlyContinue
}
"Starting service"
foreach ($string in $ServiceDisplayName)
{
    if ((get-service -DisplayName $string -ErrorAction SilentlyContinue).status -eq 'Stopped')
    {
        Start-Service -DisplayName $string -ErrorAction SilentlyContinue
    }
}
Edited by hatharry
  • Like 1
Link to comment
Share on other sites

FrostByte

This was done real quick and I know it can be improved upon, but it seems to work during my quick test for checking the update level defined by the user and then grabs the appropriate version

# Download installer
"Downloading installer"
$filename = 'setup.exe'
$SUL = Select-String $env:USERPROFILE\AppData\Roaming\Emby-Server\config\System.xml -pattern "SystemUpdateLevel"
if ($SUL -match "Release") {$level = "release"}
if ($SUL -match "Beta") {$level = "beta"}
if ($SUL -match "Dev") {$level = "dev"}
$source = "http://mb3admin.com/downloads/$level/server/$filename"
$destination = $downloadPath + $fileName
Invoke-WebRequest $source -OutFile $destination​

Edited by FrostByte
Link to comment
Share on other sites

Thanks FrostByte.

I've used the first script for a few updates now.

 

How about this for getting update level? 

$InstallFolder = 'MediaBrowser-Server', 'Emby-Server'

#Find install folder
foreach ($string in $InstallFolder)
    { if (Test-Path -Path $env:USERPROFILE\AppData\Roaming\$string ) 
        {$InstallFolder = $string}
    }


...........

# Download installer
write-host "Download installer"
$filename = 'setup.exe'
$SUL = Select-String $env:USERPROFILE\AppData\Roaming\$InstallFolder\config\System.xml -pattern "SystemUpdateLevel"
if ($SUL -match "Release") {$level = "release"}
if ($SUL -match "Beta") {$level = "beta"}
if ($SUL -match "Dev") {$level = "dev"}
$source = "http://mb3admin.com/downloads/$level/server/$filename"
$destination = "$downloadPath" + "\" + "$fileName"
Invoke-WebRequest $source -OutFile $destination​

Also, if you ever run into permissions issues, this at the top of the script will auto-elevate and relaunch:

(I have this only because I'm running other commands later that need elevation)

#Test for admin permissions
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   
#"No Administrative rights, it will display a popup window asking user for Admin rights"

$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments

break
}

-timotl

Edited by timotl
  • Like 1
Link to comment
Share on other sites

FrostByte

Ya, that looks good.  I know there are some people who still have the old install folder because they haven't installed fresh in awhile and wasn't sure how to check for that.  I'm really not good at coding, just threw that together and I know it really was kind of ugly. 

Link to comment
Share on other sites

  • 2 weeks later...
FrostByte

Just a small batch file to go along with the script above to make things as automated as possible.  Just place the batch file in the same folder as the script file while using the same filename for both (use whatever folder and filename you wish).  Then you can create a shortcut to the batch file on your desktop if you wish and run that whenever you're ready to install a new version of server.  This script automatically checks the server version, environment variables, admin rights, etc so it hopefully should work for everyone running Emby as a service.  

 

Thanks to everyone who contributed to the code, especially Quicky who wrote most of the code and got this started.

 

Example batch filename

 

C:\Utils\EmbyServerUpdate.bat

@ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"

Example script filename:

 

C:\Utils\EmbyServerUpdate.ps1​​

#Test for admin permissions
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   
#"No Administrative rights, it will display a popup window asking user for Admin rights"

$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments

break
}

$ServiceDisplayName = 'Media Browser', 'Emby Server'
$ServiceName = 'MediaBrowser', 'Emby'
$ProcessName = 'MediaBrowser.ServerApplication'
# Installer download path
$downloadPath = "$env:USERPROFILE\Downloads"
$InstallFolder = 'MediaBrowser-Server', 'Emby-Server'

#Find install folder
foreach ($string in $InstallFolder)
    { if (Test-Path -Path $env:USERPROFILE\AppData\Roaming\$string ) 
        {$InstallFolder = $string}
    }

# Stop the emby service
"Stopping service"
foreach ($string in $ServiceDisplayName)
{
    Stop-Service -DisplayName $string -ErrorAction SilentlyContinue
}
Stop-Process -Name $ProcessName -ErrorAction SilentlyContinue -Force
Wait-Process -Name $ProcessName -ErrorAction SilentlyContinue

# Download installer
write-host "Downloading installer"
$filename = 'setup.exe'
$SUL = Select-String $env:USERPROFILE\AppData\Roaming\$InstallFolder\config\System.xml -pattern "SystemUpdateLevel"
if ($SUL -match "Release") {$level = "release"}
if ($SUL -match "Beta") {$level = "beta"}
if ($SUL -match "Dev") {$level = "dev"}
$source = "http://mb3admin.com/downloads/$level/server/$filename"
$destination = "$downloadPath" + "\" + "$fileName"
Invoke-WebRequest $source -OutFile $destination

# Run installer
write-host "Running installer"
& $destination | Out-Null
"Waiting for emby"
$process = get-Process -Name $ProcessName -ErrorAction SilentlyContinue
while (!$process)
{
    $process = get-Process -Name $ProcessName -ErrorAction SilentlyContinue
}

# Kill the emby app that gets started.
write-host "Finding emby service"
foreach ($string in $ServiceDisplayName)
{
    if (Get-Service -DisplayName $string -ErrorAction SilentlyContinue)
    {
        $emby = Get-Service -DisplayName $string -ErrorAction SilentlyContinue
    }
}
while (!$emby)
{
    # Check the app is running. Wait until it starts
    foreach ($string in $ServiceDisplayName)
    {
        if (Get-Service -DisplayName $string -ErrorAction SilentlyContinue)
        {
            $emby = Get-Service -DisplayName $string -ErrorAction SilentlyContinue
        }
    }
}

#Close emby
write-host "Closing emby"
Stop-Process -Name $ProcessName -ErrorAction SilentlyContinue -Force
Wait-Process -Name $ProcessName -ErrorAction SilentlyContinue

# Delete downloaded installer
write-host "Deleting installer"
Remove-Item $destination

#Restart the emby service
write-host "Setting service to auto startup"
foreach ($string in $ServiceName)
{
    Set-Service -Name $string -StartupType Automatic -ErrorAction SilentlyContinue
}

#Start service
write-host "Starting service"
foreach ($string in $ServiceDisplayName)
{
    Start-Service -DisplayName $string -ErrorAction SilentlyContinue
}

#Complete
write-host "Completed emby update"


Edited by FrostByte
  • Like 1
Link to comment
Share on other sites

jhoff80

This is interesting and all, but it seems to me that it's way more complicated than it has to be, isn't it?  I mean unless you're doing it this specific way for a reason.  The same setup.exe always will download the newest version of whichever branch you're on  so it doesn't need to be downloaded each time.

 

Really all you need to do is stop the service, run the saved exe, close the app version, and restart the service.

 

This all seems like a bit of overkill to me.
 

  • Like 1
Link to comment
Share on other sites

FrostByte

Of course it can (see post #1).  However, what if you switch between levels as many people do?  What if you don't want to save the setup.exe file(s) on your system? 

 

The first version by Quicky was simple and works just fine if you want to modify a few lines for your needs.  The version I posted though was written to automatically check for ALL possibilities and be used by everyone without modifying.  Plus I had nothing else to do :) 

Link to comment
Share on other sites

Koleckai Silvestri

Thanks. This works perfectly for me on Windows Server Essentials 2016 preview 3. Thinking about setting it up as a Schedule Task to check for updates every morning at 4 am or something like that.

Edited by Koleckai Silvestri
  • Like 1
Link to comment
Share on other sites

  • 1 month later...
xtremecool

Are the beta installers still being uploaded here?

 

    http://mb3admin.com/downloads/beta/server/setup.exe

 

The last version that this script is picking up is 5804, which is now 6 versions out of date.

 

I can see the betas are being put onto GitHub but as a zip, and the contents of this zip appears to be an updated System directory.

 

If GitHub is now the new place to get the beta files, is it safe to assume that the contents of the zip file can be extracted to the root of the Emby-Server directory in the AppData\Roaming directory?

 

Also if any additional directories are required to be updated by a given beta, will those updated directories also be included in the beta zip file?

 

If the answer is yes to both I'll see if I can update the PowerShell script to handle this change and post the results when I get it working.

Link to comment
Share on other sites

Koleckai Silvestri

I've updated my copy of the script to use this URL:

 

https://github.com/MediaBrowser/Emby.Releases/raw/master/Server/windows/$level/$filename
  • Like 2
Link to comment
Share on other sites

Overseer

 

I've updated my copy of the script to use this URL:

https://github.com/MediaBrowser/Emby.Releases/raw/master/Server/windows/$level/$filename

 

I've got mine doing the same. Working well with that so far.

Link to comment
Share on other sites

  • 2 weeks later...

Cheers for your input FrostByte, hatharry, timotl & other contributers - I've replaced my original script with the one that's developed here and it works great. I don't browse the forums often (purely because emby works pretty much flawlessly for me), but I love this sort of crowd-sourced evolution!

  • Like 1
Link to comment
Share on other sites

  • 1 year later...
  • 2 weeks later...
xtremecool

I get the same error.

 

I've not managed to figure out how to get to the latest dev setup.exe, assuming it still is available somewhere in GitHub.

 

I guess I'll have to start downloading the latest dev version and figure out how to manually blend my installation with the contents of the zip file.

 

Pity, as this script made updating pretty painless.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...