Managing a Minecraft Server With Windows PowerShell.

So, my friends and I run our own little MC Server and we decided to automate Boot up and shutdown. Thus I was tasked with writing a .ps1 file to do just that. I Present to You MCServerMgr.ps1! This file manages the MC server based on the time of day. Basically, if the server stops 9am-12pm, It'll Reboot the server. If the server stops from 12pm-8:59am, It shuts down the host PC. The times were simply chosen because we decided 9:00am was a good time to start the server, and midnight was a good time to stop it. Here Is the Code I Wrote: (I recommend copy and pasting this into PowerShell ISE, VS Code, or Notepad++)

 

##-- \/\/ EDIT THESE VARIABLES \/\/ --##

$ServerPath = "C:\Users\Johnny\Desktop\SimplyMagicServer\SimplyMagicServer\forge-1.7.10-10.13.4.1614-1.7.10-universal.jar"
$StopTime = "9" ##<<Must be 24 Hour Format--##

##-- /\/\ EDIT THESE VARIABLES /\/\ --##
##-- DO NOT EDIT BELOW THIS LINE --##
Function Boot{
$Time = $Null
Write-Host "Rebooting Server" -ForegroundColor Green
java -Xms2048m -Xmx4096m -XX:PermSize=256m -jar $ServerPath nogui
Start-Sleep -s 5
Time 
}
Function Time{
Write-host "Server Stopped. TimeStamp:" -ForegroundColor Red
$Time = Get-Date -format %H
Write-host $Time -ForegroundColor Red

if([Int]$Time -lt [Int]$StopTime){
Write-Host "After Midnight. Shutting Down." -ForegroundColor Magenta
Stop-Computer $env:COMPUTERNAME
Pause
}

Else{
Boot
}
}

Write-Host "Minecraft Server Manager by: John Heisler Has Been Started..." -ForegroundColor Yellow
Boot

Comments

Popular posts from this blog

Create Profiles for Managing Microsoft Exchange using Windows Terminal (Online and On-Prem)

Give a User 'SendAs' permssions to a Mail-Enabled AD Group in Exchange Online

Get a User's Active Directory Groups