Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tasks/windows.json
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
}
}
}
19 changes: 17 additions & 2 deletions tasks/windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ param(

[Parameter(Mandatory = $false)]
[bool]
$FailOnFail=$true
$FailOnFail=$true,

[Parameter(Mandatory = $false)]
[String]
$Provider="cmd"
)

function write_error($Message, $ExitCode){
Expand All @@ -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
}
Expand Down
Loading