Deprecated version! Please see renewed program Unified Scheduler Service
This project has an aim to allow System Administrators and also to who Thinks to be System Administrator to launch Power Shell scripts as Windows Service.
When you install and then start this Windows Service, it scans default folder in its root PowerShellScripts, then serches for default script StartScript.ps1 in parent directories:
.
├── ...
├── PowerShellScripts
| ├── SomeStuff_1
| | ├── ...
| | ├── StartScript.ps1
| | └── ...
| ├── SomeStuff_2
| | ├── ...
| | ├── StartScript.ps1
| | └── ...
| └── ...
└── ...
To each script following command parameters are being passed:
myCommand.Parameters.Add(new CommandParameter("Automated", true));
myCommand.Parameters.Add(new CommandParameter("CurrentDateTimeUtc", DateTime.UtcNow.ToString("o")));
which you can retrieve on the script side this way:
[CmdletBinding()]
param (
[switch]$Automated,
[string]$CurrentDateTime
)
if($CurrentDateTime) {
[datetime]$CurrentDateTime = [datetime]::parseexact($CurrentDateTime, 'dd/MM/yyyy HH:mm:ss', $null)
}
Write-Host "Automated: $Automated" -ForegroundColor Green
Write-Host "CurrentDateTime: $CurrentDateTime" -ForegroundColor Green
Thanks to that, it's possible to create standalone scripts or automated scheduled scripts, which will be executed according to the script managed schedule logic.
Every script is launched in its own thread, so if one crashes, others are able to run anyway:
Power Shell Scripts Service Thread
└── DoWork Thread
├── SomeStuff_1 / StartScript.ps1 Thread
├── SomeStuff_2 / StartScript.ps1 Thread
└── ...
It's set to execute only signed scrips by default, but if you don't care about your environment security, it's possible to launch them in unrestricted mode.
Continue to read to see other possible settings...
Here are all currently available customizations, managed into handy json file:
{
"ServiceName": "PSScriptsService",
"Description": "Windows service, which allows you to invoke PowerShell Scripts",
"DisplayName": "PowerShell Scripts Service",
"LogPath": "",
"LogSize": "20",
"ScriptsPath": "",
"TargetScript": "",
"SignedScripts": true
}
Let's see each one:
- ServiceName - System service name. I suggest to use short names without spaces or other strange characters. See What are valid characters in a Windows service (key) name?.
- Description - Description you wants to give to this service. Just put something very serious and technically complex to admire what kind of DUDE you are!
- DisplayName - Same thing like for ServiceName, but you are free to use spaces.
- LogPath - If empty, the service will create Logs folder in its root.
- LogSize - Integer Megabytes.
- ScriptsPath - If empty, the service will check PowerShellScripts folder in its root.
- TargetScript - Normally service is looking for default StartScript.ps1, you are free to specify another entry point script name.
- SignedScripts - true for AllSigned or false for Unrestricted
You can create multiple services just create another service root folder and set different: ServiceName, Description, DisplayName; also: LogPath, ScriptsPath; in case of custom values.
I have prepared 2 *.cmd files to simplify service system integration:
Install.cmd
"%~dp0PSScriptsService.exe" install
pause
and
Uninstall.cmd
"%~dp0PSScriptsService.exe" uninstall
pause
These *.cmd files have to be launched with Admin privileges.
After installation you have to start your newly created windows service: Win+R -> services.msc -> Enter -> Search by DisplayName.
One of the possible usages of this solution is to run Node js Express (React js Prerendering Server) and .Net Core WebApi on windows machines.
StartScript.ps1
Start-Process -FilePath "npm" -Args "run-script webpack_prod" -WorkingDirectory "C:\source\repos\SomeProject\code\frontend" -NoNewWindow -Wait
Start-Process -FilePath "npm" -Args "run-script start" -WorkingDirectory "C:\source\repos\SomeProject\code\frontend" -NoNewWindow
Start-Process -FilePath "dotnet" -Args "run --configuration Release --project C:\source\repos\SomeProject\code\backend\SomeProject.csproj" -NoNewWindow
This simple script will be launched in Power Shell Scripts Service context, build and start you frontend npm bundle and backend WebApi.