Any PowerShell script could be remodeled to a actual Windows service that runs within the background and begins routinely throughout your server boot. You can create a Windows service utilizing
instsrv.exe instruments (from Windows Server Resource 2003 Kit) which let you run the
powershell.exe course of with a parameter that incorporates the trail to your PS1 script file.
The fundamental drawback of making a service utilizing this technique is that srvany.exe doesn’t management a PowerShell script execution state, and if the app crashes (hangs up), the service doesn’t see it and goes on working. To create a Windows service from a file that incorporates a PowerShell script, on this article we’ll use the NSSM (Non-Sucking Service Manager) toolkit, which doesn’t display the above talked about disadvantages.
You can obtain and set up NSSM manually or utilizing Chocolatey. Firstly, set up Choco itself:
Set-ExecutionPolicy Bypass -Scope Process -Force; `
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/set up.ps1'))
Then set up the NSSM package deal:
choco set up nssm
In this instance, we’ll monitor the adjustments in a particular Active Directory group in actual time and inform a safety administrator utilizing a and e-mail (the script is given in ) .
So, now we have a PowerShell code that must be saved as a PS1 file. Let’s add an infinite loop that performs a verify each minute:
You can create a service from a PowerShell script utilizing NSSM immediately from PowerShell:
$NSSMPath = (Get-Command "C:psnssmwin64nssm.exe").Source
$NewServiceName = “CheckADGroup”
$PoShPath= (Get-Command powershell).Source
$PoShScriptPath = “C:psCheckADGroupcheckad.ps1”
$args = '-ExecutionPolicy Bypass -NoProfile -File ""' -f $PoShScriptPath
& $NSSMPath set up $NewServiceName $PoShPath $args
& $NSSMPath standing $NewServiceName
Start your new service:
Check the service standing in PowerShell:
So you may have created and began your new Windows service. Make certain that it has appeared within the providers administration console (providers.msc).
CheckADGroup has appeared, it’s configured to start out routinely and is at present working. As you may see, your PowerShell script is working contained in the nssm.exe course of.
In order the service can present notifications in a consumer session allow the Allow service to work together with desktop possibility on the Log on tab.
Start-Service -Name ui0detect
However, Interactive Services Detection Service has been fully faraway from Windows 10 construct 1803, and also you gained’t be capable of swap to Session zero. So you gained’t see the notification home windows displayed beneath System account.
You can change the service description utilizing this command:
& $NSSMPath set $NewServiceName description “Monitoring of AD group adjustments”
To take away the service you may have created, use the
sc delete command or:
nssm take away CheckADGroup