From e75ee6e842cb06daf4e5f80b00e31be8a3d2e8c9 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 Fixes #1203 Co-Authored-By: taloth --- .../ProcessProviderFixture.cs | 51 ++++++++++++++++++- .../Processes/ProcessProvider.cs | 10 ++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/NzbDrone.Common.Test/ProcessProviderFixture.cs b/src/NzbDrone.Common.Test/ProcessProviderFixture.cs index f0802d10d..6951e2c42 100644 --- a/src/NzbDrone.Common.Test/ProcessProviderFixture.cs +++ b/src/NzbDrone.Common.Test/ProcessProviderFixture.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.ComponentModel; using System.Diagnostics; using System.IO; @@ -102,6 +102,55 @@ namespace NzbDrone.Common.Test Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should().BeFalse(); } + [Test] + [Explicit] + 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] [Platform(Exclude = "MacOsX")] [Retry(3)] diff --git a/src/NzbDrone.Common/Processes/ProcessProvider.cs b/src/NzbDrone.Common/Processes/ProcessProvider.cs index 103cdc183..b558c8a31 100644 --- a/src/NzbDrone.Common/Processes/ProcessProvider.cs +++ b/src/NzbDrone.Common/Processes/ProcessProvider.cs @@ -379,6 +379,16 @@ namespace NzbDrone.Common.Processes 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); } }