From df59c3e59750edcffe1aa780accd94d850a9a491 Mon Sep 17 00:00:00 2001 From: Shubham Shinde Date: Thu, 14 Aug 2025 18:45:09 +0530 Subject: [PATCH] (MODULES-11579) Add support for powershell CLI to exec::windows This adds support for provider parameter which current support 'cmd' and 'powershell'(Default: cmd). When provider=powershell, the command provided to the exec task will be run on a powershell terminal. --- README.md | 3 +++ tasks/windows.json | 4 ++++ tasks/windows.ps1 | 19 +++++++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2e2d2dc..c44c59a 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ For example, to view the free disk space of a host, run: This example is specifically for Windows using Powershell and returns a list of features installed on the server: `puppet task run exec command='powershell -command "Get-WindowsFeature | Where Installed | Format-List -Property Name"' --nodes neptune` +* For exec::windows task, CLI provider can also be passed using the 'provider' parameter. Supported CLIs are cmd and powershell, with default being cmd. +`puppet task run exec provider=powershell command="Get-WindowsFeature | Where Installed | Format-List -Property Name" --nodes neptune` + You can also run tasks in the PE console. See PE task documentation for complete information. ## Reference diff --git a/tasks/windows.json b/tasks/windows.json index 1ae560f..3958594 100644 --- a/tasks/windows.json +++ b/tasks/windows.json @@ -6,6 +6,10 @@ "command": { "description": "The command to run, including all arguments.", "type": "String[1]" + }, + "provider": { + "description": "The CLI provider to run the command on. Supported providers are 'cmd' and 'powershell'. Default is 'cmd'.", + "type": "Optional[String]" } } } diff --git a/tasks/windows.ps1 b/tasks/windows.ps1 index 307f54d..3caca2f 100644 --- a/tasks/windows.ps1 +++ b/tasks/windows.ps1 @@ -11,7 +11,11 @@ param( [Parameter(Mandatory = $false)] [bool] - $FailOnFail=$true + $FailOnFail=$true, + + [Parameter(Mandatory = $false)] + [String] + $Provider="cmd" ) function write_error($Message, $ExitCode){ @@ -34,7 +38,18 @@ if ($Interleave -eq $true){ $Redirect = "2>&1" } -$CommandOutput = cmd /c $Command $Redirect +if ($Provider -eq "cmd") { + $CommandOutput = cmd /c $Command $Redirect +} elseif ($Provider -eq "powershell") { + if ($Interleave -eq $true) { + $CommandOutput = powershell -Command $Command 2>&1 + } else { + $CommandOutput = powershell -Command $Command + } +} else { + write_error -Message "Unsupported provider: $Provider" -ExitCode 1 +} + if ($LASTEXITCODE -eq 0){ echo $CommandOutput }