-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunServer.ps1
71 lines (51 loc) · 3.69 KB
/
RunServer.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#The purpose of this script is to make the running and changing of server settings much easier. Since there are multiple files that have to be run, I wanted to make a single file that does all of that plus allow you to change server settings.
$ErrorActionPreference = "Stop"
#Takes the media directories that the user specifies and runs the main server.
function Start-Server($mediaDir, $mediaIconDir) {
Write-Host "Starting node server"
Invoke-Command -ScriptBlock{nodemon -i .\scripts\info app.js $mediaDir $mediaIconDir -w ..\}
for ($true) {
}
}
try {
cd $PSScriptRoot #Changes directory to the script directory which is in the root folder of the server
Import-Module .\Settings.psm1 -Force #imports custom functions/modules I made (seperate file)
Import-Module .\scripts\getFileIcons.psm1 -Force
$Settings = Get-Settings "./settings.txt" -Force #function to read from a settings.txt and format them into a dictionary/object with keys and values.
$defaultRes = Read-Host -Prompt "Default Configuration (y or n)?" #Reads response from user
if ($defaultRes.ToLower() -ne "y" -and $defaultRes.ToLower() -ne "n") {
for ($defaultRes.ToLower() -ne "y" -and $defaultRes.ToLower() -ne "n") { #Repeats until the user replies with y or n
$defaultRes = Read-Host -Prompt "Default Configuration (y or n)?"
if($defaultRes.ToLower() -eq "y" -or $defaultRes.ToLower() -eq "n") {
break
}
}
}
$script = "./scripts/getFileIcons.ps1"
$mongoScript = "C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe"
if ($defaultRes -eq "y") { #if the response is yes, use the default settings that were assigned, and start each component of the server asynchronously
$mediaDir = $Settings.Default.MediaDir
$mediaIconDir = $Settings.Default.MediaIconDir
Start-Job -ScriptBlock{param($mediaIconDir)Import-Module .\scripts\getFileIcons.psm1 -Force; Get-FileIcons -mediaIconDir $mediaIconDir} -ArgumentList $mediaIconDir
Start-Job -ScriptBlock{param($mongoScript)Invoke-Command -ScriptBlock {param($mongoScript)cd C:/; cmd.exe /C $mongoScript} -ArgumentList $mongoScript} -ArgumentList $mongoScript
Start-Sleep -Seconds 3
Start-Server -mediaDir $mediaDir -mediaIconDir $mediaIconDir
} elseif ($defaultRes -eq "n") { #if the response is no, ask for new settings
$mediaDir = Read-Host -Prompt "Media Directory (backslash only)"
$mediaIconDir = Read-Host -Prompt "Media Icons Directory (backslash only)"
$newConfig = Read-Host -Prompt "Would you like to save these settings as default? (y or n)" #Asks user if they want to save the new settings as default
if ($newConfig -eq "y") { #if yes, set the settings to default, and run a function that saves it to the file in a specified format to be able to be edited by file if chosen.
$Settings.Default.MediaDir = $mediaDir
$Settings.Default.MediaIconDir = $mediaIconDir
Set-Settings settings.txt $Settings
}
Start-Job -ScriptBlock{${functionGet-FileIcons -mediaIconDir $mediaIconDir}} #After saving or not saving the settings, it starts the server and its components (Note to self, I will have to put these in functions as I repeated this twice)
Start-Job -ScriptBlock{param($mongoScript)Invoke-Command -ScriptBlock {param($mongoScript)cd C:/; cmd.exe /C $mongoScript} -ArgumentList $mongoScript} -ArgumentList $mongoScript
Start-Sleep -Seconds 3
Start-Server -mediaDir $mediaDir -mediaIconDir $mediaIconDir
}
} catch {throw $_}
finally { #After everything, stop the jobs, and remove jobs or error
Stop-Job *
Remove-Job *
}