Windows Service Deployment Automation: PowerShell script for deploying Windows Service

Recently, I was working on deployment of a Windows Service on multiple in instances. It involves multiple steps & was time consuming process.  So, I thought of creating a PowerShell script for automating the Windows Service deployment. In this post, I will share how we can create a simple PowerShell script for doing the same.

Step 1: Stop & delete the service if already existing

if (Get-Service -Name $ServiceName -ErrorAction SilentlyContinue)
{
Stop-Service -Name $ServiceName

sc.exe delete $ServiceName
}

Here:

$ServiceName : Name of the Service which we want to manage/deploy.

Step 2 (optional): Set System Environment Variables required by the Service

In my case, Service require ASPNETCORE_ENVIRONMENT system environment variable to be set with value “Production

[System.Environment]::SetEnvironmentVariable('ASPNETCORE_ENVIRONMENT','Production',[System.EnvironmentVariableTarget]::Machine)

Step 3 (optional): Modify service configuration settings

In my case, I need to modify appsettings.Production.json file & replace a setting value of “InstanceId” from “<PlaceholderInstanceId>” with the one supplied by user as argument.

(Get-Content -Path $ServiceConfig -Raw) -Replace "<PlaceholderInstanceId>", $args[0] | Set-Content -Path $ServiceConfig

Here:

$ServiceConfig: Path of appsettings.Production.json file

Step 4 (optional): Copy files to Service folder

In my case, I need to copy a instance config file to Service root folder

Copy-Item $InstanceConfig -Destination $ServiceRootPath

Here:

$InstanceConfig: Path of instance config file

$ServiceRootPath: Path of Service Root Folder

Step 5: Add new Service

New-Service -Name $ServiceName -BinaryPathName $ServicePath -Description $ServiceDescription

Here:

$ServiceName: Name of the Service

$ServicePath: Service Root Folder Path

$ServiceDescription: Service Description

Step 6 (optional): Set Service Start Type to “Automatic”

Set-Service -Name $ServiceName -StartupType Automatic

Step 7: Start the Service

Start-Service -Name $ServiceName

Step 8: Check Service Status

Get-Service -Name $ServiceName

Step 9: Full sample PowerShell script for deploying the Windows Service (SampleWindowsService.ps1)

if($args.count -eq 1)
{
  $ServiceName = "MyTestService"
  
  $ServiceRootPath = "D:\MyTestService\"

  $ServicePath = $ServiceRootPath + "\MyTestService.exe"

  $ServiceDescription = "My Test Service"
  
  $ServiceConfig = $ServiceRootPath + "\appsettings.Production.json"
  
  $InstanceConfig = "D:\InstanceConfig.config"
  
  (Get-Content -Path $ServiceConfig -Raw) -Replace "<PlaceholderInstanceId>", $args[0] | Set-Content -Path $ServiceConfig
  
  Copy-Item $InstanceConfig -Destination $ServiceRootPath

  if (Get-Service -Name $ServiceName -ErrorAction SilentlyContinue)
  {
    Stop-Service -Name $ServiceName

    sc.exe delete $ServiceName
  }

  New-Service -Name $ServiceName -BinaryPathName $ServicePath -Description $ServiceDescription

  [System.Environment]::SetEnvironmentVariable('ASPNETCORE_ENVIRONMENT','Production',[System.EnvironmentVariableTarget]::Machine)

  Set-Service -Name $ServiceName -StartupType Automatic

  Start-Service -Name $ServiceName

  Get-Service -Name $ServiceName
}
else
{
  $Message = "Need to specify InstanceId argument"
  write-output $Message 
}

Step 10: Execute script

Powershell windows service deploymnet

Step 11: Open “Services” to verify

Services MyTestService

That it !!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *