问题
I'm using Rundeck version 3.0.13 to send powershell commands over WinRM to Windows Server 2016 hosts. Is there any way to run a powershell command with pipe characters as a single command? For example, I'd love to be able to execute a command like this to start non-disabled services:
Get-Service | where-object {$_.StartType -ne "Disabled"} | foreach-object {Start-Service $_}
But I get the error back "where-object" command not found. The WinRM node executor runs the command as an argument to powershell in a CMD prompt, so what is happening is that the Get-Service part of the command is run in Powershell but then it tries to pipe to "where-object" in the CMD prompt, like this:
CMD> powershell Get-Service | where-object ...
Is there any way to escape the powershell statement in Rundeck so that the pipe commands run in powershell instead of only the first part? I tried enclosing it in -command "& {...}" to no avail. (In that case I get a syntax error and the powershell help screen as a result.)
Thank you!
回答1:
The most uncomplicated way to do that is calling a script as @gravity says or use inline script step, select new "Script - Execute an inline script" step, click on "Advanced" button set "powershell.exe" in "Invocation String" and ".ps1" at "File Extension" textbox (and of course your Powershell stuff on "Enter the entire script to execute" box).
If the remote script execution is disabled in your windows node, you can enable it with set-executionpolicy remotesigned
on your windows node.
Update Answer:
If you want to execute directly on "command step" (because security policies are restricted as you said), make sure that in your project configuration is configured with "WinRM Node Executor Python" (on "Default Node Executor"), define "Powershell" on "Shell" textbox. Also, define "WinRM Node Executor Python" on "Default File Copier" as good practice.
Now you don't need to call "poweshell.exe" in your command, just put your pipe command, for example, Get-Content myfile.txt | Select-string -SimpleMatch "${option.mystring}"
.
回答2:
You have to setup proper node definitions, is your project using a mixed bag of windows and Linux nodes or just windows nodes?
Is your rundeck server windows? If yes set set your project executing mode to local, or use WinRm node executor python. Have a look at the following node definition, I'm using pywinrm to execute against a windows node with credential delegation enabled and set the default shell to powershell, using this config I can run second hop queries and pipes as one liners.
<node name="server.WinRMPython"
description="JUMP"
tags="Server 2019,WinRMPython"
hostname="myserver" osArch="amd64"
osFamily="windows"
osName="Server 2019"
osVersion="2019"
role="jump server"
function="rundeck execution node"
username="my user"
winrm-password-option="winrmPassword"
winrm-password-storage-path="keys/windows/myserver.pass"
node-executor="WinRMPython"
file-copier="WinRMcpPython"
Winrm-protocol="http"
winrm-cmd="PowerShell"
winrm-auth-type="credssp"
file-copy-destination-dir="c:\temp"/>
来源:https://stackoverflow.com/questions/58102017/how-to-run-a-powershell-command-in-rundeck-with-pipes