1. The forum will be closing soon permanently. Please read the announcement here

    Note: User registration has been closed. We do not accept any new accounts.

[Script & Tutorial] Dedicated Server Automatic & Manual Updater - BETA 0.95

Discussion in 'Multiplayer' started by gimmilfactory, Feb 23, 2016.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. gimmilfactory

    gimmilfactory Junior Engineer

    Messages:
    523
    Space Engineers Automatic & Manual Dedicated Server Updating Script v0.95 - Complete with Setup Tutorial!


    I hope you guys will enjoy, and utilize it!!!!....Until something else better either comes
    along or it becomes obsolete. Most certainly however, I do plan on adding on to it,
    shortening the code in a few places and making it more robust overall.

    Download: (Updated 03-4-16)
    Current Version 0.95 - .ps1 PowerShell Script File - Left Click to view as plain text. Right Click > Save link as.. to download.

    Change Log: (v0.95) *Like Keen, not always complete*
    - Added support for Multiple Instances.
    - Added "Manual Updating" method to Tutorial.
    - Added "Local / Console" Save directory enable-ability & Configuration Value.
    - Added 12 hr "AM/PM" to default time formatting.
    - Added handling of a few more Exceptions. There are most certainly one or two more ways to break the current version.
    - Added more Dev-Commenting to the script code so what is going on a little easier to understand.
    - Changed the version numbering to better respect the state of the code :).
    - Changed Exit Status Codes.
    - Changed how world saves are formatted and stored; to bring it up-to-par with multi-instance support.
    - Updated "Manual Run" output messages.
    - Updated and added to Tutorial.

    Known Issues: (v0.95)
    - Using "Local / Console" along with other multiple instances is not currently supported.
    - Task Scheduler Debug Exit Status are missing from Tutorial (I'll add them soon-ish).

    Planned Changes/Features:
    - (more of a note-to-self) The formatting grabber has ugly code in the aliases(config) section. Hide it later.
    - Thinking about programming an "Instance Auto-Detect" feature into the coding which is toggle-able.

    Prerequisites:
    In order for this automation script to work, there are a few things you will need:
    A. PowerShell - Version 1.0. (This comes installed on all PCs: Windows 7/Server 2003 and later.) If somehow you do not have it; the latest version "4" can be found on Microsoft's website - Here.
    B. Task Scheduler - Also Installed with every version of Windows.
    C. At least - some - scripting knowledge. (I can't answer everyone's questions when they shouldn't be messing with this stuff if they haven't the clue as to what they're doing.)
    D. A copy of my script (other author's scripts may/may not work).
    E. Space Engineers (I'm sure this can be adapted easily to other games).
    F. A Network-Accessible: Locally-Hosted or VPN-Tunneled Server. ( I will figure out how to handle and write the a version of this script that will work with SteamCMD; be patient!)
    G. Have your game's directory sub-folder "Tools" shared and accessible on your Network + Server. - If you do not know how to do this - Use This Tutorial.
    H. Some time to spare.


    Steps of the Script:

    1. Makes sure File-Folders for value strings are correct. - If they are do not exist - It will exit with a Status Code 12.
    2. Makes sure Remote folder and Zip-File exists, is accessible, and folder value strings are correct. - If one is wrong - It will exit with a Status Code 12.
    3. Checks if there is a Local Zip-File for the server file comparison to work -If one does not exist, it will then create a brand-new and empty Zip-File then ask you to re-run script. - It will exit with a Status Code 5.
    (This is a safety step I put in for both not accidentally updating your server if you do not want to, but trying to simply setup the script.)
    4. Compares Remote "Date Modified" value for the Remote Server Zip File - To the Local Server Zip File.
    -A. If they DO match, it will exit the script without running the update process - Stopping at this step and exiting with a Status Code 2. (Update Not Needed)
    -B. If they DO NOT match, it will continue with the script and run the update - Continues to Step 4. (Update Needed)
    5. Backs-up the Current Local Server File over to the Backup Directory & Renames it with formatted time and date, following original file name.
    6. Copies the Remote Server File over to the Local Storage Directory.
    7. Stops Single or Multiple Server Instances.
    8. Creates a new folder for backing up your world save(s), formatted with a date and time; in the Backup Directory.
    9. Copies your current World-Save File to the newly created and dated Backup Folder.
    10. Deletes ONLY the 3 outdated folders in your Server Installation Directory, that your Server Files are comprised of. (This means that it is safe to store the script in there, among other files.)
    11. Extracts new Server File Contents to the Server Installation Directory.
    12. Starts Single or Multiple Server Instances.
    13. Profit!!! - It will exit with a Status Code 1.


    Configuring the Script:

    The script can be edited in any text editor. While editing the code, you will only ever have to deal with lines 16 through 49; these are the variable strings we will be changing, and date formatting for the saving procedure.

    Taking a Quick Look at the Code we're Interested in

    Code:
    ##
    ## Variables ##
     
    # Dedicated Server Instance Name(s)
    $InstanceNames = @("Server 1","Server 2")
    # Multiple server Server Example
    # $InstanceNames = @("Server 1","Server 2","Server 3","Server Etc...")
     
    # Remote Space Engineers Directory Location
    $RemoteDir = "\\GAMINGPC\Tools"
     
    # Local Zip File Storage Directory Location
    $ZipDir = "C:\Server\Storage"
     
    # Zip File Name
    ## DO NOT EDIT!! ## This is Currently a Hard-Coded File-Name for Space Engineers Game #
    $Zip = "DedicatedServer.zip"
     
    # Space Engineers Dedicated Server Installation Directory
    $InstallDir = "C:\Server\SEDS"
     
    # World-Save Backup Directory Location
    $BackupDir = "C:\Server\Backups"
     
    # World-Save Storage Directory
    # ## DO NOT EDIT!! ## This is a Hard-Coded Location for Space Engineers Game #
    $GSavesDir = "C:\ProgramData\SpaceEngineersDedicated"
     
    # Local / Console Save Directory!! RARE INSTANCES !!
    #$GSavesDir = "%appdata%\SpaceEngineers"
    # !! If Space Engineers is saving worlds to User's storage location instead of ProgramData, swap the "#" sign in the "$GSavesDir" codes above.
     
    # Current System Time-Stamp Grabber ##
    $CTime = (Get-Date).tostring(
     
    # Custom Date Formatting #
    # Edit This # !! KEEP QUOTES !! #
    "MM-dd-yyyy--hh_mm_ss-tt"
     
    # Keep this right parenthesis! #
    )

    Editing the Script

    1.
    Server Instance Name(s)
    Change the "Dedicated Server Instance Name(s)" field variable to your server's Instance Name(s) - This is the same name that is provided within the server instance selection menu.
    Code:
    # Dedicated Server Instance Name(s)
    $InstanceNames = @("Server 1","Server 2")
    # Multiple server Server Example
    # $InstanceNames = @("Server 1","Server 2","Server 3","Server Etc...")
    [​IMG]

    2. There are 4 folder string values you will need to change:
    2. Cont. The script will exit with an error (See step 9) if any of these are set incorrectly.

    - Remote Space Engineers Directory Location.
    ^ Shared "Tools" folder located in gaming pc's game directory. **** MAPPED NETWORK DRIVE LOCATIONS(E.G. Z:\), WILL NOT WORK ****
    Code:
    # Remote Space Engineers Directory Location
    $RemoteDir = "\\GAMINGPC\Tools"
    - Local Zip File Storage Directory Location.
    ^ Where you transfer the remote server-zip-file for the server version checking part of the script to work, and for quicker extraction purposes.
    ^ Seriously, never try to copy tons of random files over a network, it is mayhem for your network, and slow.
    Code:
    # Local Zip File Storage Directory Location
    $ZipDir = "C:\Server\Storage"
    - Space Engineers Dedicated Server Installation Directory
    ^ Where you keep your server installed at. I have my server files located at: "C:\Server\SEDS".
    Code:
    # Space Engineers Dedicated Server Installation Directory
    $InstallDir = "C:\Server\SEDS"
    - World-Save Backup Directory Location
    ^ The script backs up your world before it updates. This is the location where they will be stored.
    ^ The current DedicatedServer.zip file is also moved to the backup directory's root folder, and has it's name is updated with the backup time-stamp.
    Code:
    # World-Save Backup Directory Location
    $BackupDir = "C:\Server\Backups"
    3. BONUS ROUND!!!
    - World-Save Storage Directory ***Normally never edited***
    ^ The World-Saves location for the server is hard-coded to the game(mentioned in script comments), and only needs to be changed in rare instances.
    ^ This would be the case if you used the Default "Local / Console" option, as shown in Part 1. Also, if you do use this option, it will break multi-instances.
    ^ Simply swap the Hash-Tag/Pound-Sign "#" symbol in the two parts of code for "$GSavesDir".
    capabilities (I find a way to fix this at some point).
    Code:
    # World-Save Storage Directory
    # ## DO NOT EDIT!! ## This is a Hard-Coded Location for Space Engineers Game #
    $GSavesDir = "C:\ProgramData\SpaceEngineersDedicated"
     
    # Local / Console Save Directory!! RARE INSTANCES !!
    #$GSavesDir = "%appdata%\SpaceEngineers"
    # !! If Space Engineers is saving worlds to User's storage location instead of ProgramData, swap the "#" sign in the "$GSavesDir" codes above.
    4. Change the date format grabber - I will hide the formatting string-bits for this part of the configuration, later.
    ^ This is or your world-save backup folder naming scheme, formatted to however you wish it to be. However, just remember to follow the formatting rules it has been written in. (Underscore and dashes are accepted; you can re-arrange the month and day depending on your country.)
    The FULL Formatting Scheme can be found Here. (You have to scroll down a just over half way to the "Custom Formatting" section.)
    Code:
    # Current System Time-Stamp Grabber ##
    $CTime = (Get-Date).tostring(
     
     
    # Custom Date Formatting #
    # Edit This # !! KEEP QUOTES !! #
    "MM-dd-yyyy--hh_mm_ss-tt"
     
     
    # Keep this right parenthesis! #
    )

    Setting-Up Manual Updating:

    1. For this you want to be viewing your Main Desktop.
    2. Right Click on any empty space, Go to: New > Shortcut.
    [​IMG]

    3. In the opened dialog box, we will be pasting the powershell code needed for the script to launch.
    3. Cont. Enter The Following(Replacing the part following "-File", with the location you've stored your script):
    powershell.exe -ExecutionPolicy Bypass -NoProfile -File C:\Server\Update-Server-v0.95.ps1
    [​IMG]

    4. Click "Next", then name your Shortcut.
    [​IMG]

    5. Click Finish, You should now see the powershell icon with your shortcut name.
    [​IMG]

    6. Run it once to show the following message:
    [​IMG]

    7. Run whenever you want to update your server manually!
    7. Cont. Any Errors you come across from the setup and run process, will output full debug dialog output to the window so that you can fix the error.




    Setting-Up Auto Update via the Task Scheduler:

    Start Menu
    1.
    Start out by hitting your "Windows Key" or click on the "Start Menu".

    2. Simply start typing the words "Task Scheduler". Hitting enter while it is highlighted or clicking on it, will open the program.
    [​IMG]

    Task Scheduler - Task Scheduler Library
    3.
    Here we have the Task Scheduler Program. By clicking on the "Task Scheduler Library", we can pull up the interface system we are interested in viewing and controlling.

    Task Scheduler - Create Task
    4.
    Now that we have our main view open, Right-Click on the "Task Scheduler Library" in the left window pane, and Click "Create Task...".
    [​IMG]

    Task Scheduler - Create Task - General Tab
    5. A new "Create Task" window will have appeared. Here in the "General" tab section, we will be interested in a few items.

    5A. "Name"; name it whatever you'd like.

    5B. "Security Options" - We will need to select the "Run whether user is logged on or not" option, and the click the "Run with highest privileges" tick-box.
    [​IMG]

    Task Scheduler - Create Task - Triggers Tab
    6. Next navigate to the "New Trigger" tab at the top of our "Create Task" window; Click "New".

    6A. The "Begin the task" section needs to be "On a Schedule" through the drop-down menu.

    6B. The task will need to run "Weekly".

    6C. "Start" date needs to be the upcoming 'Thursday' (At time of writing this, it was 2/22, 2/25 was the following Thursday).
    6C. Cont. If today IS a Thursday, Leave it set to today's date(technically in the past if it is after noon or an update) so that it will run the following day that it needs to, for the patch.

    6D. "Start" time needs to be set accordingly. I let my script run every 30 minutes for 6 hours. My time-zone is: EST (Eastern Standard Time) UTC/GMT -5 hours. I have seen 10AM releases, and 6PM releases of this game; so a 6 hour window seemed to be a good value; user-preference.
    6D. Cont. So What we will be setting here, is what time we want the script to start it's initial run for the day.. Mine runs at 1200 hours -5GMT, what ever that is your time, would be a good place to set yours.

    6E. The script needs to recur every "1 weeks", on "Thursdays" and "Fridays". (These are the days the game is Updated, then patched).

    6F. "Repeat task every" tick-box needs to be selected; the following value can be adjusted. I personally use 30 minutes, but how ever often is user-preference.

    6G. "for a duration of" - Off to the right of "Repeat task every" - is used to off-set for how long "repeat task every X" is done for.

    6H. As a percussion for hanging scripts, I additionally set "Stop task if it runs longer for" to "10 minutes".

    6I. Click "OK".

    6Z. BONUS ROUND!!! It is possible to set up multiple triggers. I have a second one setup which is Triggered by "On Startup". And has "Delay task for:" set to "5 minutes".
    6Z. Cont. This allows the server to always ensure that the server is up-to-date, but usually never needed; it only helps in cases if you have your server off for a few days.
    [​IMG]

    Task Scheduler - Create Task - Actions Tab
    7.
    In the "Actions Tab" of the "Create Task" window. Click "New".
    7A. "Action:" Needs to be set to "Start a program".
    7B. "Settings" Section - The "Program/script:" needs to have the value "powershell.exe" placed in the value field.
    7B. Cont. If the full path is required, search your computer for where the powershell.exe is located, and enter it. (mine is "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"). This sometimes happens if the path isn't entered into your system's environment variables(advanced system stuff).

    7C. The - "Add arguments (optional):" - Setting, we will be using. Here is where our main script values are placed in. ****It is also very important to note, that the full field can't all be seen at one time.****

    7C. Cont. Enter into this field exactly as follows(with no space at beginning): -ExecutionPolicy Bypass -Noninteractive -Noprofile -File C:\Update-Server-v0.95.ps1
    7C. Cont.
    At the end after "-File", you will want to fill in the location where you've placed your script file. Mine is located in "C:\Server"; So: "-File C:\Server\Update_Server-v0.95.ps1"

    7D. Click "OK".
    [​IMG]

    Task Scheduler - Create Task - Settings Tab
    8. Next head over to the "Settings" tab.
    8A. Click on the tick-box labeled "Run task as soon as possible after a scheduled start is missed".
    [​IMG]

    Task Scheduler - Finishing the Configuration
    9.
    From here, you can simply click "OK"; that's it for configuration!
    9A. However, since we have set the task to run without logging in (in the General Tab), you will be prompted for the user's password. Enter it, and click "OK".
    [​IMG]

    Task Scheduler - Initially Running the Script
    9.
    We've found ourselves back at the Task Scheduler main window. Here we can see our newly created task. We have to run it to make sure it works.
    9. Cont. As a note however, this page does not update/refresh automatically. Each time you run your task, you must update the page by either Swiftly hitting "F5" on your keyboard, or going to Action > Refresh at the top of the window.

    9A. Right-Click on your newly created task, And click "Run". It will display one of the following exampled images:
    9A. Cont. If you are too slow with refreshing, and don't need an update with all being well, it won't show the "The task is currently running. (0x41301)" in the "Last Run Result" column; it will of run already and exited with a status exit code.
    [​IMG]

    9B. Refresh the window, and you should see a "(0x3)" Last Run Result. This means that the script has done all of it's setup checks, has created it's fake comparison file; at this point your auto updating script is 100% operational!!!

    9C. Run it again to actually initialize an update, or leave it alone until an update day.
    [​IMG]


    Task Scheduler - Debug and Exit Messages (INCOMPLETE)
    10A.
    Viewing a "(0x1)" in the "Last Run Result" column, means that an update has ran, and the script exited successfully.
    [​IMG]

    10B. Viewing a "(0x2)" in the "Last Run Result" column, means that there is currently no update available, and the script exited successfully.
    [​IMG]

    10C. Viewing "The system cannot find the file specified." in the "Last Run Result" column, means that task scheduler could not find "powershell.exe" for the "program/script" value field in the "Actions Tab". Either check the spelling or see part: 7B Cont.
    [​IMG]


    10D. Viewing a "(0xFFFD0000)" in the "Last Run Result" column, means that task scheduler's "additional arguments" value field in the "Actions Tab" has something wrong in it.
    [​IMG]


    11. PROFIT!!!!!!!! Confirming that the script is running correctly, it will always send out a "0x2" (no update needed), if ran at any given time.

    The next time the game updates on your gaming PC and the task scheduler runs the script, it will be the last time that you never will of had to update your sever manually ever again.

    Enjoy.

    Tutorial written on: 3/4/2016 - Script Version 0.95.
     
    Last edited: Mar 11, 2016
    • Disagree Disagree x 1
  2. gimmilfactory

    gimmilfactory Junior Engineer

    Messages:
    523
    Updated to version 0.95

    3/11/2016
    Fixes:
    - Fixed Tutorial: Manual Start. It was missing the command "-NoProfile" in the shortcut, causing an exception while trying to start your server instances.
     
    Last edited: Mar 11, 2016
    • Disagree Disagree x 1
Thread Status:
This last post in this thread was made more than 31 days old.