Skip to content

Commit

Permalink
Escaping [ for Get-Service
Browse files Browse the repository at this point in the history
  • Loading branch information
John Simons committed Jul 3, 2017
1 parent c1f67f4 commit 3752237
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public override void SetUp()
[TearDown]
public override void CleanUp()
{
//DeleteExistingService();
// base.CleanUp();
DeleteExistingService();
base.CleanUp();
}

protected abstract string ServiceName { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Calamari.Tests.Fixtures.Deployment
[Category(TestEnvironment.CompatibleOS.Windows)]
public class DeployWindowsServiceWithCustomServiceNameFixture : DeployWindowsServiceAbstractFixture
{
protected override string ServiceName => @"foo$bar";
protected override string ServiceName => @"[f`o]o$b[a]r";

[Test]
public void ShouldEscapeBackslashesAndDollarSignsInArgumentsPassedToScExe()
Expand All @@ -18,5 +18,14 @@ public void ShouldEscapeBackslashesAndDollarSignsInArgumentsPassedToScExe()
Variables["Octopus.Action.WindowsService.Description"] = @"""c:\foo $dr bar\"" ArgumentWithoutSpace";
RunDeployment();
}

[Test]
public void ShouldUpdateExistingServiceWithFunnyCharactersInServiceName()
{
RunDeployment();

//Simulate an update
RunDeployment();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,27 @@ function WrapInQuotes([string]$arg){
"`"$arg`""
}

function EscapeArgumentForPS([string]$arg){

## Escape all ` with another ` (this is needed so PS does not get confused with variables)
$arg = $arg.Replace("``", "````");

## Escape all [ with a ` (this is needed so PS does not get confused with variables)
$arg = $arg.Replace("[", "``[");

## Escape all $ with a ` (this is needed so PS does not get confused with variables)
$arg = $arg.Replace("$", "`$");

return $arg
}

function EscapeArgumentForConsole([string]$arg){
## For all ", double the number of \ immediately preceding the "
$arg = $arg -replace "(\\+)+`"",'$1$1"'

## Add a single \ preceding all "
$arg = $arg.Replace("`"", "\`"");

## Escape all $ with a ` (this is needed so PS does not get confused with variables)
$arg = $arg.Replace("$", "`$");

## if string ends with \, double the number of all ending \
$arg = $arg -replace "(\\+)+$",'$1$1"'

Expand Down Expand Up @@ -70,7 +81,6 @@ else
$binPath = (EscapeArgumentForConsole $fullPath)
}


$fullArguments = @((WrapInQuotes (EscapeArgumentForConsole $serviceName)), "binPath=", $binPath)
if ($displayName)
{
Expand Down Expand Up @@ -111,7 +121,9 @@ else
}
}

$service = Get-Service $serviceName -ErrorAction SilentlyContinue
$psServiceName = (EscapeArgumentForPS $serviceName)

$service = Get-Service $psServiceName -ErrorAction SilentlyContinue

if (!$service)
{
Expand All @@ -124,7 +136,7 @@ if (!$service)
throw "sc.exe create failed with exit code: $LastExitCode"
}

$service = Get-Service $serviceName -ErrorAction SilentlyContinue
$service = Get-Service $psServiceName -ErrorAction SilentlyContinue
}
else
{
Expand All @@ -133,7 +145,7 @@ else
If ($service.Status -ne 'Stopped')
{
Write-Host "Stopping the $serviceName service"
Stop-Service $ServiceName -Force
Stop-Service $psServiceName -Force
## Wait up to 30 seconds for the service to stop
$service.WaitForStatus('Stopped', '00:00:30')
If ($service.Status -ne 'Stopped')
Expand Down Expand Up @@ -180,7 +192,7 @@ elseif ($startMode -eq "demand")
else
{
Write-Host "Starting the $serviceName service"
Start-Service $ServiceName
Start-Service $psServiceName

$service.WaitForStatus('Running', '00:00:30')
If ($service.Status -ne 'Running')
Expand Down

0 comments on commit 3752237

Please sign in to comment.