Error running PowerShell Command from Python Subprocess

0

When I try to run following python PowerShell command :

p1 = subprocess.run([‘powershell’,‘-Command’,‘Add-DistributionGroupMember -identity “Staff” -member “eng.hassan”’], capture_output = True)

I got folloiwng error :

‘Add-DistributionGroupMember’ is not recognized as an internal or external command, operable program or batch file.

other PowerShell commands executed successfully like (Get-ADuesr), I think the error cause this command is part of Exchange online module not the Active directory commands.

What happens if you type that command in a CMD terminal window?
I do mean CMD and not a POWERSHELL window.

The command is:
``
`powershell -Command Add-DistributionGroupMember -identity “Staff” -member “eng.hassan”


I expect that you will get the same error message.

Maybe you have to install something to have the Add-DistributionGroupMember powershell command?

Try having all of your arguments in a list, as individual strings, and not one string with spaces.

>>> var = subprocess.run(['powershell', '-command', 'get-filehash', 'f:/fix.txt', '-algorithm', 'sha1'], capture_output = True, encoding = 'utf-8')
>>> print(var.stdout)

Algorithm       Hash                                                                   Path
---------       ----                                                                   ----
SHA1            6BB7AB29F706BE5B04B0FD9543059EBBE7E14DB3                               F:\fix.txt