From ae47ee817fc28c1576eb47326782c162d89bf5f1 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Tue, 2 Jul 2019 20:50:32 +0200 Subject: [PATCH] Fixed: Executing powershell and python scripts directly in Connect->Custom Scripts --- .../ProcessProviderTests.cs | 48 +++++++++++++++++++ .../Processes/ProcessProvider.cs | 10 ++++ 2 files changed, 58 insertions(+) diff --git a/src/NzbDrone.Common.Test/ProcessProviderTests.cs b/src/NzbDrone.Common.Test/ProcessProviderTests.cs index b411b1cb4..3bbe5a7d7 100644 --- a/src/NzbDrone.Common.Test/ProcessProviderTests.cs +++ b/src/NzbDrone.Common.Test/ProcessProviderTests.cs @@ -87,6 +87,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] [Ignore("Shit appveyor")] diff --git a/src/NzbDrone.Common/Processes/ProcessProvider.cs b/src/NzbDrone.Common/Processes/ProcessProvider.cs index 85637e888..2e52895b2 100644 --- a/src/NzbDrone.Common/Processes/ProcessProvider.cs +++ b/src/NzbDrone.Common/Processes/ProcessProvider.cs @@ -365,6 +365,16 @@ private List 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); } }