SHARE
TWEET

Untitled

a guest Jan 9th, 2014 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Script courtesy of TnT
  2. #Version 1.0.2
  3. #This script will launch your server and zip up your server directory into a folder of your choice upon the server stopping.
  4. #No warranty is provided whatsoever.  Learn this script and what it does before using it.
  5.  
  6. #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#   BEGIN STATIC VARIABLE DECLARATION   #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
  7. #Set the minimum PowerShell version this script works with. (DO NOT CHANGE).
  8. New-Variable -Name POWERSHELL_VERSION -Option Constant -Value ([int]3)
  9. #Set the minimum .NET Framework version this script works with. (DO NOT CHANGE).
  10. New-Variable -Name FRAMEWORK_VERSION -Option Constant -Value ([decimal]4.5)
  11.  
  12. #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#   BEGIN POWERSHELL CUSTOMIZATION   #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
  13. $Host.UI.RawUI.WindowTitle = "PowerLaunch"
  14.  
  15. #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#   BEGIN FUNCTION DECLARATION   #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
  16.  
  17. #Function to create the config.txt file.  If the file exists, it will not be overwrote.
  18. Function Create-Config() {
  19.     If (!(Test-Path config.txt)) {
  20.         $currentlocation=Get-Location
  21.         $parentfolder=(get-item $currentlocation).parent.FullName
  22.         $backupfolder = Join-Path -path $parentfolder -childpath backup
  23.         New-Item config.txt -ItemType "file"
  24.         Add-Content config.txt "SERVER_NAME=MyServer"
  25.         Add-Content config.txt "SERVER_LOCATION=$currentlocation"
  26.         Add-Content config.txt "BACKUP_LOCATION=$backupfolder"
  27.         Add-Content config.txt "CRAFTBUKKIT=craftbukkit.jar"
  28.         Add-Content config.txt "JAVA_FLAGS=-Xmx1G"
  29.         Add-Content config.txt "CRAFTBUKKIT_OPTIONS=-o True -p 25565"
  30.         Add-Content config.txt "TEST_DEPENDENCIES=True"
  31.         Add-Content config.txt "DELETE_LOG=False"
  32.         Add-Content config.txt "TAKE_BACKUP=True"
  33.         Add-Content config.txt "RESTART_PAUSE=5"
  34.     }
  35. }
  36.  
  37. #Loads all variables from the config.txt file.
  38. Function Load-Variables() {
  39.     $types = @{
  40.     SERVER_NAME         = {$args[0] -as [string]}
  41.     SERVER_LOCATION     = {$args[0] -as [string]}
  42.     BACKUP_LOCATION     = {$args[0] -as [string]}
  43.     CRAFTBUKKIT         = {$args[0] -as [string]}
  44.     JAVA_FLAGS          = {
  45.         ($args[0].split(' ')[0] -as [string]),
  46.         ($args[0].split(' ')[1] -as [string]),
  47.         ($args[0].split(' ')[2] -as [string]),
  48.         ($args[0].split(' ')[3] -as [string]),
  49.         ($args[0].split(' ')[4] -as [string]),
  50.         ($args[0].split(' ')[5] -as [string]),
  51.         ($args[0].split(' ')[6] -as [string]),
  52.         ($args[0].split(' ')[7] -as [string]),
  53.         ($args[0].split(' ')[8] -as [string])
  54.     }
  55.     CRAFTBUKKIT_OPTIONS = {
  56.         ($args[0].split(' ')[0] -as [string]),
  57.         ([bool]::Parse($args[0].split(' ')[1])),
  58.         ($args[0].split(' ')[2] -as [string]),
  59.         ($args[0].split(' ')[3] -as [int])
  60.     }
  61.     TEST_DEPENDENCIES   = {([bool]::Parse($args[0]))}
  62.     DELETE_LOG          = {([bool]::Parse($args[0]))}
  63.     TAKE_BACKUP         = {([bool]::Parse($args[0]))}
  64.     RESTART_PAUSE       = {$args[0] -as [int]}
  65.     }
  66.    
  67.     $ht = [ordered]@{}
  68.     Get-Content config.txt |
  69.     ForEach-Object {
  70.         $parts = $_.split('=').trim()
  71.         $ht[$parts[0]] = &$types[$parts[0]] $parts[1]
  72.     }
  73.     New-object PSObject -Property $ht
  74. }
  75.  
  76. #Function to ensure the correct version of PowerShell can be found to run this script.
  77. #If the wrong version is found, it will provide a link to the correct version.
  78. Function Test-PowerShell([int]$minVersion) {
  79.     $psVer = $PSVersionTable.PSVersion.Major
  80.     If ($psVer -ge $minVersion) {
  81.         Write-Host -foreground Green "PowerShell version $psVer is compatible"
  82.         Return $true
  83.     }
  84.    
  85.     Write-Host -foreground Red "PowerShell version $psVer is NOT compatible"
  86.     Write-Host -foreground Red "You need to install PowerShell v$minVersion or greater."
  87.     Write-Host -foreground Red "Get Powershell 4.0 here (Requires Windows 7 SP1 or Windows Server 2008 R2 SP1):"
  88.     Write-Host -foreground Red "http://www.microsoft.com/en-us/download/details.aspx?id=40855"
  89.     Write-Host -foreground Red "Aborting script"
  90.    
  91.     Return $false
  92. }
  93.  
  94. #Function to ensure the correct version of .NET Framework is found to run this script.
  95. #If an older version is found, it will provide a link to the correct version.
  96. Function Test-Framework([decimal]$minVersion) {
  97.     $frameworkVer = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client'
  98.     $frameworkVer = $frameworkVer.Version
  99.     If ($frameworkVer -ge $minVersion) {
  100.         Write-Host -foreground Green ".NET Framework $frameworkVer is compatible."
  101.         Return $true
  102.     }
  103.    
  104.     Write-Host -foreground Red ".NET Framework version $frameworkVer is NOT compatible"
  105.     Write-Host -foreground Red "You need to install .NET Framework $minVersion or greater."
  106.     Write-Host -foreground Red "Get .NET Framework $minVersion here (Requires Windows 7 SP1 or Windows Server 2008 R2 SP1):"
  107.     Write-Host -foreground Red "http://www.microsoft.com/en-ca/download/details.aspx?id=30653"
  108.     Write-Host -foreground Red "Aborting script"
  109.    
  110.     Return $false
  111. }
  112.  
  113. #Function to launch the server.  Arguments passed into this is Function is the directory of the server, the location of the server jar file
  114. #the Java flags to use and any command line options to pass to the server.
  115. Function Launch-Server([string]$location, [string]$jarfile, [array]$flags, [array]$options) {
  116.     If (($location -eq "") -or ($location -eq $NULL)) {
  117.         Write-Host ""
  118.         Write-Host -foreground Red "Launch Aborted.  Server folder has not been specified."
  119.         Return
  120.     }
  121.    
  122.     If (!(Test-Path $location)) {
  123.         Write-Host ""
  124.         Write-Host -foreground Red "No server folder $location found.  You must create this folder yourself.  Launch aborted."
  125.         Return
  126.     }
  127.  
  128.     $jarfound = (Get-ChildItem $location -filter "*.jar").Name
  129.     If (($jarfile -eq "") -or ($jarfile -eq $NULL)) {
  130.         Write-Host ""
  131.         Write-Host -foreground Red "CraftBukkit executable $jarfile not found, however, we did find:"
  132.         Write-Host -foreground Red "$jarfound"  
  133.         Write-Host -foreground Red "Do you have a typo in your config.txt?"
  134.         Return
  135.     }
  136.  
  137.     $server = Join-Path -path $location -childpath $jarfile
  138.     $runlocation = Get-Location
  139.     If (!(Test-Path $server)) {
  140.         If ($jarfound -ne $NULL) {
  141.             Write-Host -foreground Red "$server not found, however, we did find:"
  142.             Write-Host -foreground Red "$jarfound"  
  143.             Write-Host -foreground Red "Do you have a typo in your config.txt?"
  144.             Return
  145.         }
  146.         Write-Host -foreground Red "$server not found.  Aborting."
  147.     }
  148.    
  149.     Write-Host "PowerLaunch - Premium CraftBukkit Start Script"
  150.     Set-Location $location
  151.     java $flags -jar $server $options
  152.     Set-Location $runlocation
  153. }
  154.  
  155. #Function to find the size of the server directory
  156. Function Get-DirectorySize() {
  157.   param ([string]$root = $(Resolve-Path .))
  158.   Get-ChildItem -re $root | ?{ -not $_.PSIsContainer } | Measure-Object -sum -property Length
  159. }
  160.  
  161. #Function to backup the contents of a directory.
  162. #This will break If not using .NET 4.5 or above
  163. Function Write-Backup([string]$backup, [string]$name, [string]$server, [int]$delay, [decimal]$size) {
  164.     $date = get-date -UFormat %Y-%m-%d-%H-%M
  165.     If (($backup -eq "") -or ($backup -eq $NULL)) {
  166.         Write-Host ""
  167.         Write-Host -foreground Red "Backup Aborted.  Backup folder has not been specified."
  168.         Return
  169.     }
  170.  
  171.     If (!(Test-Path $backup)) {
  172.         Write-Host ""
  173.         Write-Host -foreground Red "No backup folder $backup found.  You must create this folder yourself.  Aborting backup."
  174.         Return
  175.     }
  176.    
  177.     If (($server -eq "") -or ($server -eq $NULL)) {
  178.         Write-Host ""
  179.         Write-Host -foreground Red "Backup Aborted.  Server folder has not been specified."
  180.         Return
  181.     }
  182.    
  183.     If (!(Test-Path $server)) {
  184.         Write-Host ""
  185.         Write-Host -foreground Red "No server folder $server found.  Backup aborted."
  186.         Return
  187.     }  
  188.    
  189.     If (($name -eq "") -or ($name -eq $NULL)) {
  190.         $backupFile = Join-Path -path $backup -childpath ("UNNAMED" + "-" + $date + ".zip")
  191.     }
  192.     Else {
  193.         $backupFile = Join-Path -path $backup -childpath ($name + "-" + $date + ".zip")
  194.     }
  195.    
  196.     If (Test-Path $backupFile) {
  197.         Write-Host ""
  198.         Write-Host -foreground Red "Backup Aborted.  $backupFile already exists."
  199.         Return
  200.     }
  201.     Write-Host ""
  202.     Write-Host -background Red "Press q to abort backup - you have $delay seconds to decide"
  203.     Start-Sleep -s $delay
  204.  
  205.     If ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {
  206.         Write-Host -foreground Red "Key pressed. Aborting Backup."
  207.         Return
  208.     }
  209.     Write-Host -foreground Green "Backup starting.  For large server directories, this may take a while."
  210.     Write-Host -foreground Green "Size to be backed up: $size GB. Hold please."
  211.     #Dump the output to null to avoid spamming the console.  Backups will still run without issue
  212.     [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") > $NULL
  213.     $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
  214.     [System.IO.Compression.ZipFile]::CreateFromDirectory($server, $backupFile, $compressionLevel, $false)
  215.     Write-Host ""
  216.     Write-Host -foreground Green "Backup Completed.  See $backupFile"
  217. }
  218.  
  219. #Function to quit the auto relaunch based on a time set.
  220. Function Set-Quit([int]$delay) {
  221.     Write-Host ""
  222.     Write-Host -background Red "Press q to quit - you have $delay seconds to decide"
  223.     Start-Sleep -s $delay
  224.  
  225.     If ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {
  226.         Write-Host -foreground Red "Exiting Script.  Good-bye."
  227.         Return $true;
  228.     }
  229.    
  230.     Return $false
  231. }
  232.  
  233. #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#   BEGIN FUNCTION CALLS   #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
  234. #Create the config file to be used.  
  235. Create-Config
  236.  
  237. #Main area of the script.  This launches all functions above (PowerShell needs the functions declared before they can be ran).
  238. #First the dependencies get tested, then the server gets launched.  
  239. #If backups are enabled, take the backup.  Delete the server.log If that ability is enabled.
  240. #If backups are disabled, Do not take a backup or delete the server.log
  241. #Keep looping until the user quits the script.
  242. Do {
  243.     Clear-Host
  244.    
  245.     #Import Variables
  246.     $HT = Load-Variables
  247.    
  248.     If ($HT.TEST_DEPENDENCIES) {
  249.         If (!(Test-PowerShell ($POWERSHELL_VERSION))) {
  250.             Break;
  251.         }
  252.         If (!(Test-Framework ($FRAMEWORK_VERSION))) {
  253.             Break;
  254.         }
  255.     }
  256.    
  257.     Launch-Server $HT.SERVER_LOCATION $HT.CRAFTBUKKIT $HT.JAVA_FLAGS $HT.CRAFTBUKKIT_OPTIONS
  258.    
  259.     If ($HT.TAKE_BACKUP) {
  260.         $foldersize = (Get-DirectorySize $HT.SERVER_LOCATION).Sum/1gb
  261.         $roundedsize = [math]::Round($foldersize,2)
  262.         Write-Backup $HT.BACKUP_LOCATION $HT.SERVER_NAME $HT.SERVER_LOCATION $HT.RESTART_PAUSE $roundedsize
  263.         If ($HT.DELETE_LOG) {
  264.             $log = Join-Path -path $HT.SERVER_LOCATION -childpath "logs\latest.log"
  265.             If (Test-Path $log) {
  266.                 Remove-Item $log
  267.             }
  268.             Else {
  269.                 Write-Host ""
  270.                 Write-Host -foreground Red "Deleting server.log aborted.  File $log does not exists."
  271.             }
  272.         }
  273.     }
  274. } Until (Set-Quit ($HT.RESTART_PAUSE))
  275.  
  276. Write-Host "Press any key to exit"
  277. $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") > $NULL
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top