Competition: What’s your favorite Redgate tool? Enter now.

Windows Services

This script uses the Service Control tool (sc.exe) to create or update a Windows Service. It assumes some variables have been set in the Deployment Manager web interface:
# These variables should be set via the Deployment Manager web interface:
#
# ServiceName - Name of the Windows service
# ServiceExecutable - Path to the .exe containing the service
#
# sc.exe is the Service Control utility in Windows
$service = Get-Service $ServiceName -ErrorAction SilentlyContinue
$fullPath = Resolve-Path $ServiceExecutable
if (! $service)
{
  Write-Host "The service will be installed"
  New-Service -Name $ServiceName -BinaryPathName $fullPath -StartupType Automatic
}
else
{
  Write-Host "The service will be stopped and reconfigured"
  Stop-Service $ServiceName -Force
  & "sc.exe" config $service.Name binPath= $fullPath start= auto | Write-Host
}
Write-Host "Starting the service"
Start-Service $ServiceName

This script uses InstallUtil.exe to install custom Event Log sources and Windows Services:
$NetFrameworkDirectory = $([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())
& (Join-Path $NetFrameworkDirectory "InstallUtil.exe") "path\to\my.dll" | Write-Host
Justin Caldicott
Developer
Redgate Software Ltd

Comments

  • Hi,

    This first time that i work with your solutions. Is evaluating Deployment Manager now, but now need know how work others requirements...

    For example, I have 3 environments where should sync Windows Services and Task. I can syn with PowerShell Scripts only?


    Sorry for me bad english...

    Regards,




    This script uses the Service Control tool (sc.exe) to create or update a Windows Service. It assumes some variables have been set in the Deployment Manager web interface:
    # These variables should be set via the Deployment Manager web interface:
    #
    # ServiceName - Name of the Windows service
    # ServiceExecutable - Path to the .exe containing the service
    #
    # sc.exe is the Service Control utility in Windows
    $service = Get-Service $ServiceName -ErrorAction SilentlyContinue
    $fullPath = Resolve-Path $ServiceExecutable
    if (! $service)
    {
      Write-Host "The service will be installed"
      New-Service -Name $ServiceName -BinaryPathName $fullPath -StartupType Automatic
    }
    else
    {
      Write-Host "The service will be stopped and reconfigured"
      Stop-Service $ServiceName -Force
      & "sc.exe" config $service.Name binPath= $fullPath start= auto | Write-Host
    }
    Write-Host "Starting the service"
    Start-Service $ServiceName
    

    This script uses InstallUtil.exe to install custom Event Log sources and Windows Services:
    $NetFrameworkDirectory = $([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())
    & (Join-Path $NetFrameworkDirectory "InstallUtil.exe") "path\to\my.dll" | Write-Host
    
    :)
  • How execute this scripts? I've never worked with these script types...


    Regards,
    This script uses the Service Control tool (sc.exe) to create or update a Windows Service. It assumes some variables have been set in the Deployment Manager web interface:
    # These variables should be set via the Deployment Manager web interface:
    #
    # ServiceName - Name of the Windows service
    # ServiceExecutable - Path to the .exe containing the service
    #
    # sc.exe is the Service Control utility in Windows
    $service = Get-Service $ServiceName -ErrorAction SilentlyContinue
    $fullPath = Resolve-Path $ServiceExecutable
    if (! $service)
    {
      Write-Host "The service will be installed"
      New-Service -Name $ServiceName -BinaryPathName $fullPath -StartupType Automatic
    }
    else
    {
      Write-Host "The service will be stopped and reconfigured"
      Stop-Service $ServiceName -Force
      & "sc.exe" config $service.Name binPath= $fullPath start= auto | Write-Host
    }
    Write-Host "Starting the service"
    Start-Service $ServiceName
    

    This script uses InstallUtil.exe to install custom Event Log sources and Windows Services:
    $NetFrameworkDirectory = $([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())
    & (Join-Path $NetFrameworkDirectory "InstallUtil.exe") "path\to\my.dll" | Write-Host
    
  • Hi,

    You need to create a package that you want to deploy, and publish this to Deployment Manager. The PowerShell then goes into a file called Deploy.ps1, in the root of package.

    Be sure to also specify the required variables in Deployment Manager. For this script, they are:
    - ServiceName - Name of the Windows service.
    - ServiceExecutable. Path to the .exe containing the service.

    Please have a look at these sections of the documentation for details:
    http://documentation.red-gate.com/displ ... g+packages
    http://documentation.red-gate.com/displ ... deployment

    Best wishes,
    Justin Caldicott
    Developer
    Redgate Software Ltd
Sign In or Register to comment.