0
import datetime
from subprocess import PIPE

def abccompanycloudcmdrun(abccompanycloudcmd):
    abccompanycloudcmdrun_output = subprocess.run(["powershell", "-Command", abccompanycloudcmd], stdout=PIPE, stderr=PIPE,shell=True)
    return abccompanycloudcmdrun_output

if __name__ == '__main__':

    abccompanycloudcmd_success = r"abccompanycloudcmd_success_" + datetime.datetime.today().strftime('%Y-%m-%d') + ".log"
    abccompanycloudcmd_error = r"abccompanycloudcmd_error_" + datetime.datetime.today().strftime('%Y-%m-%d') + ".log"

    abccompanycloudcmd = r**'Get-ADDomainController -Filter * -Server $((Get-ADDomainController -DomainName abccompanycloud.com -Discover).hostname) ` | ft ComputerObjectDN'**
    abccompanycloudcmdrun_output = abccompanycloudcmdrun(abccompanycloudcmd)

    if abccompanycloudcmdrun_output.returncode != 0:
        print("abccompanycloudcmdrun - An error occured: %s", abccompanycloudcmdrun_output.stderr.decode("utf-8"), file = open(abccompanycloudcmd_error, 'a', encoding='utf-8', errors='ignore'))
    else:
        print("abccompanycloudcmdrun generated successfully!", file = open(abccompanycloudcmd_success, 'a', encoding='utf-8', errors='ignore'))
        print(abccompanycloudcmdrun_output.stdout.decode("utf-8"), file = open(abccompanycloudcmd_success, 'a', encoding='utf-8', errors='ignore'))

The above works fine without generating any errors in Windows. But doesnt work in Linux. It throws a message : An error occured: %s b'-Command: powershell: command not found\n'

Question is How to pass the below powershell command in python script? Get-ADDomainController -Filter * -Server $((Get-ADDomainController -DomainName abccompanycloud.com -Discover).hostname) ` | ft ComputerObjectDN

  • Running a list of tokens with `shell=True` is basically never correct. (Okay, there are scenarios where you can do it if you know what you are doing, but in the vast majority of cases, you don't want to even if you know how.) Remove the `shell=True` and you'll be fine. – tripleee Dec 13 '22 at 07:37
  • No even after removing shell=True it doesnt work as expected. bcgcloudcmdrun_output = subprocess.run(["powershell", "-Command", bcgcloudcmd], stdout=PIPE, stderr=PIPE) File "/usr/local/lib/python3.7/subprocess.py", line 800, in __init__ restore_signals, start_new_session) File "/usr/local/lib/python3.7/subprocess.py", line 1551, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'powershell': 'powershell' – Nagasri Varma Dec 13 '22 at 08:02
  • Well if you don't have `powershell` in your `PATH`, of course it will not find it. That's a separate problem. – tripleee Dec 13 '22 at 08:03
  • I tried multiple ways , but none helped. How can I find the powershell path on AWS Server? – Nagasri Varma Dec 13 '22 at 08:21
  • Did you install it in the first place? How? It's not a standard command on Linux so perhaps the real error is expecting it to be there. – tripleee Dec 13 '22 at 08:49

0 Answers0