r/PowerShell • u/maks-it • Mar 13 '20
Run PowerShell scripts as Windows service
Hi all! I just open-sourced a PSScriptsService on GitHub that lets you run scheduled PowerShell scripts as Windows service.
It creates thread timer for each found script in specified directories and passes the current utc time:
myCommand.Parameters.Add(new CommandParameter("Automated", true)); myCommand.Parameters.Add(new CommandParameter("CurrentDateTimeUtc", DateTime.UtcNow.ToString("o")));which you can retrieve on 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 GreenSchedule logic should be managed into the script, as it was an original requirement.
Let me know if you have any proposal on how to make this simple program better, more flexible and useful.
115
Upvotes
View all comments
Show parent comment
2
u/maks-it Mar 14 '20
It's up to you, your organization policy and trust level. You can leave write scripts for example to your AD, SCCM central site or branch site admins or trusted consultants, obviously less people goes there is better. Normally you should grant access who already have role to make infrastructure changes, like who makes AD users check, cleanup, recurrent creation of custom windows updates deployments.
Then if you have to use different service system users, one to work with AD and another to work with Teams, you may need to create another service instance with separate script folder and grant correct accesses accordingly to your scripts writers.