Executar a função do PowerShell a partir do script Python
Eu tenho uma necessidade para executar uma função do PowerShell de um script Python. Os arquivos .ps1 e .py estão atualmente no mesmo diretório. As funções que desejo chamar estão no script do PowerShell. A maioria das respostas que vi é para executar scripts do PowerShell inteiros do Python. Nesse caso, estou tentando executar uma função individual em um script do PowerShell a partir de um script Python.
Aqui está o exemplo do script do PowerShell:
# sample PowerShell
Function hello
{
Write-Host "Hi from the hello function : )"
}
Function bye
{
Write-Host "Goodbye"
}
Write-Host "PowerShell sample says hello."
e o script Python:
import argparse
import subprocess as sp
parser = argparse.ArgumentParser(description='Sample call to PowerShell function from Python')
parser.add_argument('--functionToCall', metavar='-f', default='hello', help='Specify function to run')
args = parser.parse_args()
psResult = sp.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
'-ExecutionPolicy',
'Unrestricted',
'. ./samplePowerShell',
args.functionToCall],
stdout = sp.PIPE,
stderr = sp.PIPE)
output, error = psResult.communicate()
rc = psResult.returncode
print "Return code given to Python script is: " + str(rc)
print "\n\nstdout:\n\n" + str(output)
print "\n\nstderr: " + str(error)
Portanto, de alguma forma, quero executar a função 'hello ()' ou 'bye ()' que está no exemplo do PowerShell. Também seria bom saber como passar parâmetros para a função. Obrigado!