1

I'm trying to get returned powershell output into slack via errbot. The bot is functioning correctly, running the code correctly and the output is being displayed in shell as expected. Can I send that returned data to slack via python code as is or do I need to return an object to return? Below I expect var x to give me the returned data, but it's obviously not.

@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
falsetru
  • 357,413
  • 63
  • 732
  • 636

1 Answers1

2

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

SIDE NOTE: Using raw string literals, you can express backslashes compactly:

>>> r"C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe" == \
... "C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"
True
falsetru
  • 357,413
  • 63
  • 732
  • 636