r/PowerShell • • 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 Green

Schedule 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

17 comments sorted by

View all comments

Show parent comment

1

u/gordonv Mar 14 '20

Yup. I am a trusted consultant. The seniors can disable scripts and permissions immediately. My scripts have a seperate set of credentials. One of them is least priviledge. I am instructed to design for this. Unfortunaltely, AD needs admin. I'm writing a 3 paragraph paper when an AD centric script needs access.

0

u/SpellCheck_Privilege Mar 14 '20

priviledge

Check your privilege.


BEEP BOOP I'm a bot. PM me to contact my author.