如何在 Rundeck 中使用管道 运行 一个 Powershell 命令

How to run a Powershell command in Rundeck with pipes

我正在使用 Rundeck 3.0.13 版通过 WinRM 将 powershell 命令发送到 Windows Server 2016 主机。有什么方法可以 运行 一个带有管道字符的 powershell 命令作为一个命令吗?例如,我希望能够执行这样的命令来启动非禁用服务:

Get-Service | where-object {$_.StartType -ne "Disabled"} | foreach-object {Start-Service $_}

但我收到错误消息 "where-object" command not found。 WinRM 节点执行器 运行 在 CMD 提示符中将命令作为 powershell 的参数,所以发生的是命令的 Get-Service 部分在 Powershell 中是 运行 但随后它尝试通过管道传输到 CMD 提示符中的 "where-object",像这样:

CMD> powershell Get-Service | where-object ... 

有什么方法可以转义 Rundeck 中的 powershell 语句,以便在 powershell 中使用管道命令 运行 而不仅仅是第一部分?我尝试将它包含在 -command "& {...}" 中但无济于事。 (在那种情况下,我得到一个语法错误和 powershell 帮助屏幕。)

谢谢!

最简单的方法是按照@gravity 所述调用脚本或使用内联脚本步骤,select 新 "Script - Execute an inline script" 步骤,单击 "Advanced" 按钮设置 [= "Invocation String" 中的 24=] 和 "File Extension" 文本框中的“.ps1”(当然还有 "Enter the entire script to execute" 框中的 Powershell 内容)。

如果在您的 windows 节点中禁用了远程脚本执行,您可以在 windows 节点上使用 set-executionpolicy remotesigned 启用它。

更新答案:

如果你想直接在"command step"上执行(因为你说的安全策略被限制),请确保在你的项目配置中配置了"WinRM Node Executor Python"(在"Default Node Executor" ), 在 "Shell" 文本框上定义 "Powershell"。此外,在 "Default File Copier" 上定义 "WinRM Node Executor Python" 作为良好做法。

现在您不需要在命令中调用 "poweshell.exe",只需输入您的管道命令,例如 Get-Content myfile.txt | Select-string -SimpleMatch "${option.mystring}".

您必须设置正确的节点定义,您的项目是混合使用 windows 和 Linux 节点还是仅使用 windows 节点?

你的运行甲板服务器windows是吗?如果是,请将您的项目执行模式设置为本地,或使用 WinRm 节点执行器 python。 看看下面的节点定义,我正在使用 pywinrm 对启用了凭据委托的 windows 节点执行并将默认值 shell 设置为 powershell,使用此配置我可以运行 第二个跃点查询和管道作为一个衬里。

  <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"/>