Skip to content

Commit cdd02cc

Browse files
(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.
1 parent 654c907 commit cdd02cc

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ For example, to view the free disk space of a host, run:
3737
This example is specifically for Windows using Powershell and returns a list of features installed on the server:
3838
`puppet task run exec command='powershell -command "Get-WindowsFeature | Where Installed | Format-List -Property Name"' --nodes neptune`
3939

40+
* For exec::windows task, CLI provider can also be passed using the 'provider' parameter. Supported CLIs are cmd and powershell, with default being cmd.
41+
`puppet task run exec provider=powershell command="Get-WindowsFeature | Where Installed | Format-List -Property Name" --nodes neptune`
42+
4043
You can also run tasks in the PE console. See PE task documentation for complete information.
4144

4245
## Reference

tasks/windows.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"command": {
77
"description": "The command to run, including all arguments.",
88
"type": "String[1]"
9+
},
10+
"provider": {
11+
"description": "The CLI provider to run the command on. Supported providers are 'cmd' and 'powershell'. Default is 'cmd'.",
12+
"type": "Optional[String]"
913
}
1014
}
1115
}

tasks/windows.ps1

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ param(
1111

1212
[Parameter(Mandatory = $false)]
1313
[bool]
14-
$FailOnFail=$true
14+
$FailOnFail=$true,
15+
16+
[Parameter(Mandatory = $false)]
17+
[String]
18+
$Provider="cmd"
1519
)
1620

1721
function write_error($Message, $ExitCode){
@@ -34,7 +38,18 @@ if ($Interleave -eq $true){
3438
$Redirect = "2>&1"
3539
}
3640

37-
$CommandOutput = cmd /c $Command $Redirect
41+
if ($Provider -eq "cmd") {
42+
$CommandOutput = cmd /c $Command $Redirect
43+
} elseif ($Provider -eq "powershell") {
44+
if ($Interleave -eq $true) {
45+
$CommandOutput = powershell -Command $Command 2>&1
46+
} else {
47+
$CommandOutput = powershell -Command $Command
48+
}
49+
} else {
50+
write_error -Message "Unsupported provider: $Provider" -ExitCode 1
51+
}
52+
3853
if ($LASTEXITCODE -eq 0){
3954
echo $CommandOutput
4055
}

0 commit comments

Comments
 (0)