sharing how I use PowerShell scripts with TopShelf for deployments using Octopus

2,037 views
Skip to first unread message

Jesse Beard

unread,
Apr 5, 2013, 1:46:15 PM4/5/13
to topshelf...@googlegroups.com
I thought I'd share this powershell script which installs TopShelf services using a custom command line and OctopusDeploy to push them out.  This works flawless with Octopus and Write-Host calls logs all the output on the Octopus side for auditing, etc.

PreDeploy.ps1

Stop-Service MyService-Service1 -ErrorAction SilentlyContinue
"MyService-Service1 is stopped" | Write-Host

Stop-Service MyService-Service2 -ErrorAction SilentlyContinue
"MyService-Service2 is stopped" | Write-Host

Stop-Service MyService-Service3 -ErrorAction SilentlyContinue
"MyService-Service3 is stopped" | Write-Host

# allow some idle time for the Windows Services to fully stop.
Start-Sleep -Second 5

"MyService Windows Services are stopped" | Write-Host

PostDeply.ps1

sc.exe delete MyService-service1 | Write-Host
sc.exe delete MyService-service2 | Write-Host
sc.exe delete MyService-service3 | Write-Host

Start-Sleep -Second 3

# install is done via TopShelf's command line to install the proper type of server.

& "C:\Octopus\Applications\$PostDeployInstallPathName\MyService.SocketServers\socketServer.exe" "/type=1" "install" | Write-Host
& "C:\Octopus\Applications\$PostDeployInstallPathName\MyService.SocketServers\socketServer.exe" "/type=2" "install" | Write-Host
& "C:\Octopus\Applications\$PostDeployInstallPathName\MyService.SocketServers\socketServer.exe" "/type=4" "install" | Write-Host

"Services are installed." | Write-Host

# configure user credentials

sc.exe config MyService-service1 obj= us...@domain.com password= "password" | Write-Host
sc.exe config MyService-service2 obj= us...@domain.com password= "password" | Write-Host
sc.exe config MyService-service3 obj= us...@domain.com password= "password" | Write-Host

"Service user credentials have been set." | Write-Host

# configure service recovery options
#
# these options tell Windows to restart the services after 1 second 
# and reset failure counts after 1 second for the service as a whole.

sc.exe failure MyService-service1 reset= 1 actions= restart/1000/restart/1000/restart/1000
sc.exe failure MyService-service2 reset= 1 actions= restart/1000/restart/1000/restart/1000
sc.exe failure MyService-service3 reset= 1 actions= restart/1000/restart/1000/restart/1000

"Service recovery options have been set." | Write-Host

# add custom command line arguments

Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\services\MyService-service1" -Name ImagePath -Value "C:\Octopus\Applications\$PostDeployInstallPathName\MyService.SocketServers\socketServer.exe /type=1 -displayname ""MyService Socket Server - service1"" -servicename ""MyService-service1""" | Write-Host
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\services\MyService-service2" -Name ImagePath -Value "C:\Octopus\Applications\$PostDeployInstallPathName\MyService.SocketServers\socketServer.exe /type=2 -displayname ""MyService Socket Server - service2"" -servicename ""MyService-service2""" | Write-Host
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\services\MyService-service3" -Name ImagePath -Value "C:\Octopus\Applications\$PostDeployInstallPathName\MyService.SocketServers\socketServer.exe /type=4 -displayname ""MyService Socket Server - service3"" -servicename ""MyService-service3""" | Write-Host

"Service custom command line arguments have been set." | Write-Host

# start the services

Start-Service MyService-service1 | Write-Host
Start-Service MyService-service2 | Write-Host
Start-Service MyService-service3 | Write-Host

Start-Sleep -Second 7

# output the status of the services

sc.exe query MyService-service1 | Write-Host
sc.exe query MyService-service2 | Write-Host
sc.exe query MyService-service3 | Write-Host

"Services have been started." | Write-Host

Wiebe Tijsma

unread,
Apr 6, 2013, 10:25:15 AM4/6/13
to topshelf...@googlegroups.com
Hi Jesse,

Thanks for sharing...

I was curious, why do you uninstall/reinstall the services everytime instead of just overwriting them, or do you have some custom installation code?

Thanks,

- Wiebe

--
You received this message because you are subscribed to the Google Groups "topshelf-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topshelf-discu...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jesse Beard

unread,
Apr 6, 2013, 11:53:17 AM4/6/13
to topshelf...@googlegroups.com
No other reason than to have each push of the service to be as clean of an install as possible.  I prefer to remove it all and reinstall each time.  I use Octopus Deploy for deployments, so all my configuration changes etc. are handled by it, so no need to worry about losing configuration settings, etc.  There is some custom install stuff happening as well so I'd rather not worry about leftovers if something changes.

Wiebe Tijsma

unread,
Apr 7, 2013, 7:41:16 AM4/7/13
to topshelf...@googlegroups.com
Ok, we did something similar before and ran into troubles that Windows wanted to restart if we uninstalled the service before a clean shutdown (but that was even before using TopShelf), so now we just stop the services, delete the folder, copy the new version and start the service again.

Thanks!

- Wiebe

Mario Pareja

unread,
Apr 8, 2013, 10:19:56 AM4/8/13
to topshelf...@googlegroups.com
Ditto.

Mario Pareja

unread,
Apr 8, 2013, 10:23:39 AM4/8/13
to topshelf...@googlegroups.com
Note, we have separate processes for fresh installs versus version upgrades to make day-to-day zero-downtime deployments nice and quick.

Jesse Beard

unread,
Apr 8, 2013, 10:59:46 AM4/8/13
to topshelf...@googlegroups.com
very interesting....

Chris Patterson

unread,
Apr 8, 2013, 9:12:49 PM4/8/13
to topshelf...@googlegroups.com
Yes, when windows loses track and goes into "service scheduled for deletion on restart" mode that's never fun. I've typically not bothered uninstalling the service either, since it's the same exe and better to stay up than have to restart the box.

Wes McClure

unread,
Feb 23, 2014, 4:37:47 AM2/23/14
to topshelf...@googlegroups.com
FWIW the "service is schedule for deletion" issue seems to only happen when I have the Services manager open
Reply all
Reply to author
Forward
0 new messages