From python I want to call a powershell script and pass a parameter to it
The Powershell function header is listed here:
Function Invoke-Neo4j
{
[cmdletBinding(SupportsShouldProcess=$false,ConfirmImpact='Low')]
param (
[Parameter(Mandatory=$false,ValueFromPipeline=$false,Position=0)]
[string]$Command = ''
)
Python - How do pass the parameter $Command = 'start' to the function from python? I can't seem to get it to work.
import subprocess
cmd = ['powershell.exe', '-ExecutionPolicy', 'RemoteSigned', '-File',
'C:\\PathName\\Invoke-Neo4j.ps1']
returncode = subprocess.call(cmd)
print(returncode)
I couldn't make the Powershell command work from Python. I actually found a .bat file with a command that works. I just inserted the 'start' parameter I call the bat file from python. It works like a charm. It's not optimal but it does the job for me.
SETLOCAL
Powershell -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -Command "try { Unblock-File -Path '%~dp0Neo4j-Management\*.*' -ErrorAction 'SilentlyContinue' } catch {};Import-Module '%~dp0Neo4j-Management.psd1'; Exit (Invoke-Neo4j 'start')"
EXIT /B %ERRORLEVEL%
Running a script, in either PowerShell or Python, that only defines a function but does not call it has no other effect than defining function. The script needs to include a specific call to that function. In your case, you might as well put the logic at the top level of the script instead of inside a function then calling the function. This would look like:
[cmdletBinding(SupportsShouldProcess=$false,ConfirmImpact='Low')]
param (
[Parameter(Mandatory=$false,ValueFromPipeline=$false,Position=0)]
[string]$Command = ''
)
# rest of code not surrounded by { }
exit 0
Invoke the revised script from Python as you were initially doing and it should work properly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.