Skip to content

Commit

Permalink
Run powershell scripts with -NoProfile if ExecuteWithoutProfile is se…
Browse files Browse the repository at this point in the history
…t and true
  • Loading branch information
Mark Rydstrom committed Jul 5, 2016
1 parent 72b6a5e commit 69be616
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions source/Calamari.Tests/Calamari.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@
<None Include="Fixtures\PowerShell\Scripts\Exit2.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Fixtures\PowerShell\Scripts\Profile.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Fixtures\PowerShell\Scripts\Parameters.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
50 changes: 50 additions & 0 deletions source/Calamari.Tests/Fixtures/PowerShell/PowerShellFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,56 @@ public void ShouldCustomizePowerShellVersionIfRequested(string customPowerShellV
}
}

[Test]
[Category(TestEnvironment.CompatibleOS.Windows)]
[TestCase("true", true)]
[TestCase("false", false)]
[TestCase("", false)]
[TestCase(null, false)]
public void ShouldCallWithNoProfileWhenVariableSet(string executeWithoutProfile, bool calledWithNoProfile)
{
var variablesFile = Path.GetTempFileName();

var variables = new VariableDictionary();
if(executeWithoutProfile != null)
variables.Set(SpecialVariables.Action.PowerShell.ExecuteWithoutProfile, executeWithoutProfile);
variables.Save(variablesFile);

using (new TemporaryFile(variablesFile))
{
var output = Invoke(Calamari()
.Action("run-script")
.Argument("script", GetFixtureResouce("Scripts", "Profile.ps1"))
.Argument("variables", variablesFile));

output.AssertSuccess();
var allOutput = string.Join(Environment.NewLine, output.CapturedOutput.Infos);
Assert.That(allOutput.Contains("-NoProfile") == calledWithNoProfile);
}
}

[Test]
[Category(TestEnvironment.CompatibleOS.Windows)]
public void ShouldNotCallWithNoProfileWhenVariableNotSet()
{
var variablesFile = Path.GetTempFileName();

var variables = new VariableDictionary();
variables.Set(SpecialVariables.Action.PowerShell.ExecuteWithoutProfile, "true");
variables.Save(variablesFile);

using (new TemporaryFile(variablesFile))
{
var output = Invoke(Calamari()
.Action("run-script")
.Argument("script", GetFixtureResouce("Scripts", "Profile.ps1"))
.Argument("variables", variablesFile));

output.AssertSuccess();
output.AssertOutput("-NoProfile");
}
}

[Test]
[Category(TestEnvironment.CompatibleOS.Windows)]
public void ShouldCallHello()
Expand Down
2 changes: 2 additions & 0 deletions source/Calamari.Tests/Fixtures/PowerShell/Scripts/Profile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# returns the commandline powershell was called with which lets us check for -NoProfile in our test
(gwmi win32_process | ? { $_.processname -eq "powershell.exe" }) | select commandline
1 change: 1 addition & 0 deletions source/Calamari/Deployment/SpecialVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public class WindowsService
public static class PowerShell
{
public static readonly string CustomPowerShellVersion = "Octopus.Action.PowerShell.CustomPowerShellVersion";
public static readonly string ExecuteWithoutProfile = "Octopus.Action.PowerShell.ExecuteWithoutProfile ";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public static string FormatCommandArguments(string bootstrapFile, CalamariVariab
{
commandArguments.Append($"-Version {customPowerShellVersion} ");
}
var executeWithoutProfile = variables[SpecialVariables.Action.PowerShell.ExecuteWithoutProfile];
bool noProfile;
if (bool.TryParse(executeWithoutProfile, out noProfile) && noProfile)
{
commandArguments.Append("-NoProfile ");
}
commandArguments.Append("-NoLogo ");
commandArguments.Append("-NonInteractive ");
commandArguments.Append("-ExecutionPolicy Unrestricted ");
Expand Down

0 comments on commit 69be616

Please sign in to comment.