Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Deployment steps for this blog post:
http://kearon.blogspot.co.uk/2015/01/installing-service-using-topshelf-with.html
function Get-ServiceExePath ($name)
{
$service = Get-WmiObject win32_service | ?{$_.Name -eq $name}
$path = $service | select @{Name="Path"; Expression={$_.PathName.split('"')[1]}}
$path.Path
}
$ServiceName = $OctopusParameters['MyServiceName']
$ExePath = Get-ServiceExePath $ServiceName
Write-Host "Removing service: " + $ServiceName
if ($ExePath)
{
Write-Host "Service executable: " + $ExePath
& $ExePath uninstall
Write-Host "Service removed."
}
else
{
Write-Host "Service not found: " + $ServiceName
}
$exe = $OctopusParameters['Octopus.Action.Package.CustomInstallationDirectory'] + '\' + $OctopusParameters['ExeName']
$serviceName = $OctopusParameters['Topshelf.ServiceName']
write-host "Installing service: " + $serviceName
write-host "Executable: " + $exe
& $exe install --autostart
Start-Service $serviceName
write-host "Service installed: " + $serviceName
@Erlord
Erlord commented Mar 16, 2016

I think the escaping in the post-deployment script might be missing? "`" instead of '' worked for me.

@cob999
cob999 commented Jun 29, 2016

this was a great help thanks, i did find that PathName is not always quoted, in which case i modifed Get-ServiceExePath to be like this

function Get-ServiceExePath ($name)
{
    $service = Get-WmiObject win32_service | ?{$_.Name -eq $name}
    if($service)
    {
        $exePath = $service.PathName
        if($exePath.startsWith('"'))
        {
            $exePath = $exePath.split('"')[1]
        }
        $exePath
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment