繁体   English   中英

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

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

我正在使用 Rundeck 版本 3.0.13 通过 WinRM 将 powershell 命令发送到 Windows Server 2016 主机。 有没有办法将 powershell 命令与 pipe 字符作为单个命令运行? 例如,我希望能够执行这样的命令来启动非禁用服务:

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

但我得到错误返回“找不到对象”命令。 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 ... 

有什么办法可以逃避 Rundeck 中的 powershell 语句,以便 pipe 命令在 powershell 中运行,而不仅仅是第一部分? 我尝试将它包含在 -command "& {...}" 中,但无济于事。 (在这种情况下,我得到一个语法错误,结果是 powershell 帮助屏幕。)

谢谢!

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

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

更新答案:

如果您想直接在“命令步骤”上执行(因为安全策略如您所说受到限制),请确保在您的项目配置中配置了“WinRM Node Executor Python”(在“默认节点执行器”上),定义“Powershell “在“外壳”文本框上。 此外,将“默认文件复制器”上的“WinRM Node Executor Python”定义为良好做法。

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

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

您的 rundeck 服务器是 windows 吗? 如果是,请将项目执行模式设置为本地,或使用 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.

  <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