Friday, June 27, 2014

Space Engineers Management Console (SEMC) v 1.2

I am very happy to announce a new version of the SEMC... I have added some very cool features and hope they work well for you.

You can download the script here (Full text copy at the bottom of this post):
http://www.denalicomputer.com/fileserver/SEMC.1.2.zip


New Features:
  • Start and stop all your servers at once 
  • Player Counts on main screen, Counts and names on each server screen
  • Show Full Log Files 
Next week I will be adding support for console instances of Space Engineers Dedicated servers. At this time only services are supported!
    Prerequisites:
    http://akmattb.blogspot.com/2014/06/space-engineers-managment-console.html
    Future Features:
    http://akmattb.blogspot.com/2014/06/space-engineers-managment-console_22.html
    Here is the text of the script:

    $versionText = "akmattb's Space Engineers Management Console v1.2"
    write-host $myinvocation.mycommand.path
    write-host $versionText
    write-host " Checking for administrator credentials."

    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
        [Security.Principal.WindowsBuiltInRole] "Administrator"))
        {
        try { 
            Write-Warning "Oops... you didn't run this as an administrator, trying to start PowerShell as administrator. Next time start the Space Engineers Management Console as administrator (Right-Click)" 
            $arg = "-file `"$($MyInvocation.mycommand.path)`""
            Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList $arg -ErrorAction 'stop' 
        }
        catch {
            Write-Warning "Error - Failed to restart script with 'run as'" 
            break              
        }
    } else {
        write-host "Finding all server instances..."
        #find all server instances
        $dedicatedPerverPath = "C:\ProgramData\SpaceEngineersDedicated\"
        $consolePerverPath = "~\AppData\Roaming\SpaceEngineersDedicated\"

        $dedicatedServers = get-childitem $dedicatedPerverPath | Where-Object {$_.PSIsContainer} | foreach-object {$_.Name}
        # $consoleServers = get-childitem $consolePerverPath | Where-Object {$_.PSIsContainer} | foreach-object {$_.Name}
        do {
            #   SELECT Server
            clear
            write-host $versionText
            write-host ""
            write-host "    Select a server to work with"
            write-host "+---------------------------------------------------------------------------------+"
            write-host ""
            $menuNumberCount = 1
            $menuOptions = @(0)
            $dedicatedServers | foreach {
                $dedicatedServerStatus = get-service -name $_
                $theLog = $dedicatedPerverPath + "\" + $_ + "\SpaceEngineers-Dedicated.log"
               
                # count users on server
                $currentPlayers = New-Object System.Collections.ArrayList
               
                #add users to users array
                $userJoined = get-content $theLog | select-string -pattern "OnConnectedPlayer"
                foreach ($user in $userJoined) {
                    [void]$currentPlayers.Add($user.line.substring(60).trimend(" attempt"))
                }
               
                #remove the people that left
                $userLeft = get-content $theLog | select-string -pattern "User left "
                foreach ($user in $userLeft) {
                    $currentPlayers.Remove($user.line.Substring(52))
                }

                $runningText = "    " + $menuNumberCount + "     " + $_ + " "
                    write-host $runningText -noNewLine
                if ($dedicatedServerStatus.Status -eq "Running") {
                    write-host "Running"  -backgroundcolor DarkGreen -foregroundcolor White  -noNewLine
                } else {
                    write-host "Stopped"  -backgroundcolor Red -foregroundcolor White -noNewLine
                }
               
                write-host " with"$currentPlayers.count "Players"
                write-host ""
               
                # increment menu numbers
                $menuOptions += ($_)
                $menuNumberCount++
            }
            # $consoleServers | foreach {}
           
            write-host "    w     Start/Restart All Servers"
            write-host ""
            write-host "    e     Stop All Servers"   
            write-host ""
            write-host "    q     Exit"   
            write-host ""
            write-host "+---------------------------------------------------------------------------------+"

            $mainMenuOption = read-host "    which server do you want to manage"
           
            switch -regex ($mainMenuOption) {
                "[1-9]" { # Working With Servers
                    if ($mainMenuOption -gt 0 -and $mainMenuOption -lt $menuNumberCount) {
                        $currentServer = $menuOptions[$mainMenuOption]
                        do { # User selected a server, lets show the other menu                       
                           
                            # Map current servers log file
                            $theLog = $dedicatedPerverPath + "\" + $currentServer + "\SpaceEngineers-Dedicated.log"
                           
                            # count users on server
                            $currentPlayers = New-Object System.Collections.ArrayList
                           
                            #add users to users array
                            $userJoined = get-content $theLog | select-string -pattern "OnConnectedPlayer"
                            foreach ($user in $userJoined) {
                                $currentPlayers.Add($user.line.substring(60).trimend(" attempt"))
                            }
                           
                            #remove the people that left
                            $userLeft = get-content $theLog | select-string -pattern "User left "
                            foreach ($user in $userLeft) {
                                $currentPlayers.Remove($user.line.Substring(52))
                            }
                            #find service name of server
                            $currentServerStatus = get-service -name $currentServer
                           
                            clear
                            write-host $versionText
                            write-host ""
                            write-host ""
                            write-host "    Working with ${currentServer} (" -noNewLine
                            if ($currentServerStatus.Status -eq "Running") {
                                write-host "Running" -backgroundcolor DarkGreen -foregroundcolor White -noNewLine
                            } else {
                                write-host "Stopped" -backgroundcolor Red -foregroundcolor White -noNewLine
                            }
                            write-host " with"$currentPlayers.count"Players)"
                            write-host "+---------------------------------------------------------------------------------+"
                            write-host ""
                            write-host "    w     Start/Restart Server"
                            write-host ""
                            write-host "    e     Stop Server"
                            write-host ""
                            write-host "    r     Edit ${currentServer} Configuration (will take effect on next server restart)"
                            write-host ""
                            write-host "    t     Open Full Log File"
                            write-host ""
                            write-host "    enter To Refresh"
                            write-host ""
                            write-host "    q     Exit to main menu"   
                            write-host ""
                            write-host "+---------------------------------------------------------------------------------+"
                            write-host ""
                            write-host "    Current Players"
                            write-host ""
                            foreach ($player in $currentPlayers) {
                                write-host "${player}, " -noNewLine
                            }
                            write-host ""
                            write-host ""
                            write-host "+---------------------------------------------------------------------------------+"
                            write-host ""
                            write-host "    Most Recent Log File Activity"
                            write-host ""
                            get-childitem $theLog  | foreach-object { write-host $_.FullName; Get-Content $_.Fullname | Select-Object -Last 10 }
                            write-host ""
                            write-host "+---------------------------------------------------------------------------------+"
                            write-host ""
                            $choice = Read-Host "    Choice"
                           
                            switch ($choice) {
                                "w" { # Start/Restart the Server
                                    restart-service $currentServerStatus
                                }
                                "e" { # Stop the Server
                                    stop-service $currentServerStatus
                                }
                                "r" { # Edit Configuration
                                    $editConfig = $dedicatedPerverPath + "\" + $currentServer + "\SpaceEngineers-Dedicated.cfg"
                                    Invoke-Item $editConfig
                                }
                                "t" { # View Full Log File
                                    Invoke-Item $theLog
                                }
                                "q" { # Back to Main menu
                                    $currentPlayers.Clear()
                                    $currentServer = 0
                                }
                            }
                        } until ($currentServer -eq 0)
                    }
                }
                "w" { #Restart all servers
                    $dedicatedServers | foreach {
                        $restartallserverslist = get-service -name $_
                        Restart-Service $restartallserverslist
                    }
                }
                "e" { #Stop all servers
                    $dedicatedServers | foreach {
                        $stopallserverslist = get-service -name $_
                        stop-service $stopallserverslist
                    }
                }
            }
        } until ($mainMenuOption -eq "q")
        clear
    }

    No comments: