powershellazure-powershelldscpowershell-dsc

How to setup automatic startup configuration for a windows service using Powershell DSC


I am building a windows server image using Packer tool and Powershell DSC. In my DSC script I am installing and starting the tomcat service as below,

Script Tomcat_Service_Install
        {
            TestScript = { $false }
            GetScript = { $null }
            SetScript = { Start-Process "C:\Program Files\apache-tomcat-X.X.XX\bin\service.bat" -ArgumentList "install" -WorkingDirectory "C:\Program Files\apache-tomcat-X.X.XX\bin" -Wait}
        }
Script Tomcat_Service_Start
        {
            TestScript = { $false }
            GetScript = { $null }
            SetScript = { Start-Process "C:\Program Files\apache-tomcat-X.X.XX\bin\tomcatX.exe" -ArgumentList "start" -WorkingDirectory "C:\Program Files\apache-tomcat-X.X.XX\bin" -Wait}
            DependsOn = "[Script]Tomcat_Service_Install"
        }

After the build completes, I logged into the system to check all the configurations. I found out that the tomcat service was installed on the system as a windows services but it was not running and the startup type was set to Manual.

Is there any way I could set the Startup type to Automatic for tomcat service using Powershell?


Solution

  • I believe you can leverage DSC service resource and try to set startup type and start the service.