简体   繁体   English

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

[英]How to run a Powershell command in Rundeck with pipes

I'm using Rundeck version 3.0.13 to send powershell commands over WinRM to Windows Server 2016 hosts.我正在使用 Rundeck 版本 3.0.13 通过 WinRM 将 powershell 命令发送到 Windows Server 2016 主机。 Is there any way to run a powershell command with pipe characters as a single command?有没有办法将 powershell 命令与 pipe 字符作为单个命令运行? 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: 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提示,像这样:

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?有什么办法可以逃避 Rundeck 中的 powershell 语句,以便 pipe 命令在 powershell 中运行,而不仅仅是第一部分? I tried enclosing it in -command "& {...}" to no avail.我尝试将它包含在 -command "& {...}" 中,但无济于事。 (In that case I get a syntax error and the powershell help screen as a result.) (在这种情况下,我得到一个语法错误,结果是 powershell 帮助屏幕。)

Thank you!谢谢!

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).最简单的方法是调用@gravity 所说的脚本或使用内联脚本步骤,select 新的“脚本 - 执行内联脚本”步骤,单击“高级”按钮在“调用字符串”中设置“powershell.exe”和“文件扩展名”文本框中的“.ps1”(当然还有“输入要执行的整个脚本”框中的 Powershell 内容)。

If the remote script execution is disabled in your windows node, you can enable it with set-executionpolicy remotesigned on your windows node.如果在 windows 节点中禁用了远程脚本执行,则可以在 windows 节点上使用set-executionpolicy remotesigned启用它。

Updated 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.如果您想直接在“命令步骤”上执行(因为安全策略如您所说受到限制),请确保在您的项目配置中配置了“WinRM Node Executor Python”(在“默认节点执行器”上),定义“Powershell “在“外壳”文本框上。 Also, define "WinRM Node Executor Python" on "Default File Copier" as good practice.此外,将“默认文件复制器”上的“WinRM Node Executor Python”定义为良好做法。

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}"现在您不需要在命令中调用“poweshell.exe”,只需输入您的 pipe 命令,例如Get-Content myfile.txt | Select-string -SimpleMatch "${option.mystring}" Get-Content myfile.txt | Select-string -SimpleMatch "${option.mystring}" . Get-Content myfile.txt | Select-string -SimpleMatch "${option.mystring}"

You have to setup proper node definitions, is your project using a mixed bag of windows and Linux nodes or just windows nodes?您必须设置正确的节点定义,您的项目是混合使用 windows 和 Linux 节点还是仅使用 windows 节点?

Is your rundeck server windows?您的 rundeck 服务器是 windows 吗? If yes set set your project executing mode to local, or use WinRm node executor python.如果是,请将项目执行模式设置为本地,或使用 WinRm 节点执行器 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. 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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM