|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +sidebar_label: Script Examples |
| 4 | +--- |
| 5 | + |
| 6 | +import Tabs from "@theme/Tabs"; |
| 7 | +import TabItem from "@theme/TabItem"; |
| 8 | + |
| 9 | +# Script Examples |
| 10 | + |
| 11 | +## Toggle Service Power State |
| 12 | + |
| 13 | +This script checks the current status of the service from the service manager and toggles it — starting the service if it's stopped, or stopping it if it's running. |
| 14 | + |
| 15 | +:::info |
| 16 | +`ServiceManagerService` requires the **Run Impersonated** option to be unchecked on the script. |
| 17 | +::: |
| 18 | + |
| 19 | +<Tabs> |
| 20 | + <TabItem value="csharp" label="C#"> |
| 21 | + |
| 22 | +```csharp |
| 23 | +//refAssemblies: TCAdmin.SDK.dll, TCAdmin.GameHosting.SDK.dll, TCAdmin.Scripting.dll, TCAdmin.Monitor.dll |
| 24 | +using TCAdmin.Web.Shared.Models.Enums; |
| 25 | + |
| 26 | +var Globals = new TCAdmin.Scripting.Engines.Addons.CSharpGameGlobals(); // DO NOT MODIFY THIS LINE |
| 27 | +
|
| 28 | +var console = Globals.ScriptConsole; |
| 29 | +var serviceManager = Globals.ServiceManagerService; |
| 30 | +var service = Globals.ThisService; |
| 31 | +var serviceManagerStatus = await serviceManager.GetServiceStatus(service.ServiceId); |
| 32 | + |
| 33 | +console.WriteLine($"Current service id is { service.ServiceId }"); |
| 34 | +console.WriteLine($"Status in database: { service.Status }"); |
| 35 | +console.WriteLine($"Status in service manager: { serviceManagerStatus.Status }"); |
| 36 | + |
| 37 | +if(serviceManagerStatus.Status == EServiceStatus.Stopped) |
| 38 | +{ |
| 39 | + await serviceManager.StartService(service.ServiceId); |
| 40 | + console.WriteLine("Service was started 🚀"); |
| 41 | +} |
| 42 | +else if(serviceManagerStatus.Status == EServiceStatus.Started) |
| 43 | +{ |
| 44 | + await serviceManager.StopService(service.ServiceId); |
| 45 | + console.WriteLine("Service was stopped 🛑"); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | + </TabItem> |
| 50 | + <TabItem value="python" label="Python 3"> |
| 51 | + |
| 52 | +```python |
| 53 | +service = ThisService |
| 54 | +serviceManagerStatus = ServiceManagerService.GetServiceStatus(service.ServiceId).Result |
| 55 | + |
| 56 | +print("Current service id is " + str(service.ServiceId)) |
| 57 | +print("Status in database: " + str(service.Status)) |
| 58 | +print("Status in service manager: " + str(serviceManagerStatus.Status)) |
| 59 | + |
| 60 | +if serviceManagerStatus.Status.ToString() == "Stopped": |
| 61 | + ServiceManagerService.StartService(service.ServiceId).Wait() |
| 62 | + print("Service was started 🚀") |
| 63 | +elif serviceManagerStatus.Status.ToString() == "Started": |
| 64 | + ServiceManagerService.StopService(service.ServiceId).Wait() |
| 65 | + print("Service was stopped 🛑") |
| 66 | +``` |
| 67 | + |
| 68 | + </TabItem> |
| 69 | + <TabItem value="powershell" label="PowerShell"> |
| 70 | + |
| 71 | +```powershell |
| 72 | +$service = $ThisService |
| 73 | +$serviceManagerStatus = $ServiceManagerService.GetServiceStatus($service.ServiceId).Result |
| 74 | +
|
| 75 | +Write-Host "Current service id is $($service.ServiceId)" |
| 76 | +Write-Host "Status in database: $($service.Status)" |
| 77 | +Write-Host "Status in service manager: $($serviceManagerStatus.Status)" |
| 78 | +
|
| 79 | +if ($serviceManagerStatus.Status.ToString() -eq "Stopped") { |
| 80 | + $ServiceManagerService.StartService($service.ServiceId).Wait() |
| 81 | + Write-Host "Service was started 🚀" |
| 82 | +} elseif ($serviceManagerStatus.Status.ToString() -eq "Started") { |
| 83 | + $ServiceManagerService.StopService($service.ServiceId).Wait() |
| 84 | + Write-Host "Service was stopped 🛑" |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | + </TabItem> |
| 89 | + <TabItem value="javascript" label="JavaScript"> |
| 90 | + |
| 91 | +```javascript |
| 92 | +var service = ThisService; |
| 93 | +var serviceManagerStatus = ServiceManagerService.GetServiceStatus(service.ServiceId).Result; |
| 94 | + |
| 95 | +Console.log("Current service id is " + service.ServiceId); |
| 96 | +Console.log("Status in database: " + service.Status); |
| 97 | +Console.log("Status in service manager: " + serviceManagerStatus.Status); |
| 98 | + |
| 99 | +// EServiceStatus: Starting=0, Started=1, Stopping=2, Stopped=3 |
| 100 | +if (serviceManagerStatus.Status == 3) { |
| 101 | + ServiceManagerService.StartService(service.ServiceId).Wait(); |
| 102 | + Console.log("Service was started 🚀"); |
| 103 | +} else if (serviceManagerStatus.Status == 1) { |
| 104 | + ServiceManagerService.StopService(service.ServiceId).Wait(); |
| 105 | + Console.log("Service was stopped 🛑"); |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | + </TabItem> |
| 110 | + <TabItem value="lua" label="Lua"> |
| 111 | + |
| 112 | +```lua |
| 113 | +local service = ThisService |
| 114 | +local serviceManager = ServiceManagerService |
| 115 | +local serviceManagerStatus = serviceManager:GetServiceStatus(service.ServiceId).Result |
| 116 | + |
| 117 | +print("Current service id is {0}", service.ServiceId) |
| 118 | +print("Status in database: {0}", service.Status) |
| 119 | +print("Status in service manager: {0}", serviceManagerStatus.Status) |
| 120 | + |
| 121 | +-- EServiceStatus: Starting=0, Started=1, Stopping=2, Stopped=3 |
| 122 | +if serviceManagerStatus.Status == 3 then |
| 123 | + serviceManager:StartService(service.ServiceId):Wait() |
| 124 | + print("Service was started 🚀") |
| 125 | +elseif serviceManagerStatus.Status == 1 then |
| 126 | + serviceManager:StopService(service.ServiceId):Wait() |
| 127 | + print("Service was stopped 🛑") |
| 128 | +end |
| 129 | +``` |
| 130 | + |
| 131 | + </TabItem> |
| 132 | +</Tabs> |
0 commit comments