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
Step 11: Open “Services” to verify
That it !!!
.NET Professional | Microsoft Certified Professional | DZone’s Most Valuable Blogger | Web Developer | Author | Blogger
Doctorate in Computer Science and Engineering
Microsoft Certified Professional (MCP) with over 12+ years of software industry experience including development, implementation & deployment of applications in the .NET framework
Experienced and skilled Agile Developer with a strong record of excellent teamwork, successful coding & project management. Specialises in problem identification and proposal of alternative solutions. Provided knowledge and individual mentoring to team members as needed
Among top 3% overall in terms of contribution on Stack Overflow (~2.3 million people reached my posts). Part of the top 1% Stack Overflow answerers in ASP.NET technology.
DZone’s Most Valuable Blogger (MVB)
Created and actively maintain the TechCartNow.com tech blog while also editing, writing, and researching topics for publication.
Excellent skills in Application Development using C#/Vb.Net, .NET Framework, ASP.NET, MVC, ADO.NET, WCF, WPF, Web API, SQL Server, jQuery, Angular, React, BackboneJS