Executable or batch-file as service in Windows with Powershell DSC

Executable or batch-file as service in Windows with Powershell DSC

In case if you ever need to create Windows service from an executable or even bat-file, please feel free to use the following configuration in Powershell DSC.

The solution based on the NSSM software which is very similar to srvany.exe (from Windows 2003 Kit). The main difference NSSM from srvany - is that srvany does not close the underlying process if the service is stopped. And NSSM does close it. Download NSSM.

Service Solr {
	Name = "Solr";
	DisplayName = "Solr AutoDeployed";
	StartupType = "Automatic";
	State = "Running";
	Ensure = "Present";
	Path = "C:\solr-6.3.0\nssm.exe";
	DependsOn = "[Environment]SetJavaHomePath", "[WindowsProcess]ExtractJava", "[Script]ExtractSolr", "[xRemoteFile]GetSrvany", "[Registry]Solr1", "[Registry]Solr2", "[Registry]Solr3";
}
Registry Solr1 {
    Key = "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Solr\Parameters";
    ValueName = "AppDirectory";
    ValueData = "c:\solr-6.3.0\bin";
    Ensure = "Present";
}
Registry Solr2 {
    Key = "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Solr\Parameters";
    ValueName = "Application";
    ValueData = "c:\solr-6.3.0\bin\solr.cmd";
    Ensure = "Present";
}
Registry Solr3 {
    Key = "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Solr\Parameters";
    ValueName = "AppParameters";
    ValueData = "start -f";
    Ensure = "Present";
}

 

Tags: Windows (EN), dsc (en)

PrintEmail

Add comment


Security code
Refresh