从 errbot 返回数据到 slack

Returning data to slack from errbot

我正在尝试通过 errbot 将 returned powershell 输出到 slack。机器人运行正常,运行 代码正确,输出按预期显示在 shell 中。我可以按原样通过 python 代码将 returned 数据发送到 slack 还是我需要 return 一个对象到 return?下面我希望 var x 给我 returned 数据,但显然不是。

@botcmd
def find_vm(self, args, SearchString):
    x = subprocess.call(["C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe", ". \"C:\Program Files\Toolbox\PowerShell Modules\vmware\./vmware.psm1\";", "find-vm", SearchString])
    return x

subprocess.call does not return the output of the command, but returns returncode of the process. You need to use other functions like subprocess.check_output:

@botcmd
def find_vm(self, args, SearchString):
    try:
        output = subprocess.check_output([
            r"C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe",
            r'. "C:\Program Files\Toolbox\PowerShell Modules\vmware\./vmware.psm1";',
            "find-vm",
            SearchString
        ])
    except subprocess.CalledProcessError:
        # Error handling
        return 'Command failed'
    return output

旁注:使用原始字符串文字,您可以简洁地表达反斜杠:

>>> r"C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe" == \
... "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe"
True