Skip to content

Commit

Permalink
Fixed: Executing powershell and python scripts directly in Connect->C…
Browse files Browse the repository at this point in the history
…ustom Scripts
  • Loading branch information
Taloth committed Jul 2, 2019
1 parent c71b4bd commit df8ca25
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/NzbDrone.Common.Test/ProcessProviderTests.cs
Expand Up @@ -77,6 +77,54 @@ public void Should_be_able_to_start_process()
Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should().BeFalse();
}

[Test]
public void Should_be_able_to_start_powershell()
{
WindowsOnly();

var tempDir = GetTempFilePath();
var tempScript = Path.Combine(tempDir, "myscript.ps1");

Directory.CreateDirectory(tempDir);

File.WriteAllText(tempScript, "Write-Output 'Hello There'\r\n");

try
{
var result = Subject.StartAndCapture(tempScript);

result.Standard.First().Content.Should().Be("Hello There");
}
catch (Win32Exception ex) when (ex.NativeErrorCode == 2)
{
Assert.Fail("No Powershell available?!?");
}
}

[Test]
public void Should_be_able_to_start_python()
{
WindowsOnly();

var tempDir = GetTempFilePath();
var tempScript = Path.Combine(tempDir, "myscript.py");

Directory.CreateDirectory(tempDir);

File.WriteAllText(tempScript, "print(\"Hello There\")\r\n");

try
{
var result = Subject.StartAndCapture(tempScript);

result.Standard.First().Content.Should().Be("Hello There");
}
catch (Win32Exception ex) when (ex.NativeErrorCode == 2)
{
Assert.Inconclusive("No Python available");
}
}


[Test]
public void kill_all_should_kill_all_process_with_name()
Expand Down
10 changes: 10 additions & 0 deletions src/NzbDrone.Common/Processes/ProcessProvider.cs
Expand Up @@ -365,6 +365,16 @@ private List<Process> GetProcessesByName(string name)
return ("cmd.exe", $"/c {path} {args}");
}

if (OsInfo.IsWindows && path.EndsWith(".ps1", StringComparison.InvariantCultureIgnoreCase))
{
return ("powershell.exe", $"-ExecutionPolicy Bypass -NoProfile -File {path} {args}");
}

if (OsInfo.IsWindows && path.EndsWith(".py", StringComparison.InvariantCultureIgnoreCase))
{
return ("python.exe", $"{path} {args}");
}

return (path, args);
}
}
Expand Down

0 comments on commit df8ca25

Please sign in to comment.