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.
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
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