none
Getting NativeCommandError while invoking Python script from Powershell! RRS feed

  • Question

  • Hi, 

    I am trying to invoke the Python script from powershell. Am passing Python command, Python script path and parameter to the Python script as you can see
    below. 

    When I am executing this, the Python script had been invoked and prcocessed as expected, but meanwhile am getting the below NativeCommandError from Powershell.
    The below output has the logs which are printed by Python the Python script. But along with that, Powershell had thrown the error.

    I just want to skip this error. However, the functnality wise there is no impact. Please suggest a way to skip this error.

    $param_data_path = 'D:\Dinesh_Kumar'
    $pyscript_path = 'D:\Dinesh_Kumar\log_rename.py'
    $python_cmd = 'D:\Models\ModelScripts\Production\python.cmd'
    
    cd $PSScriptRoot
    $cmd = "cmd.exe"
    $args = "/c $python_cmd $pyscript_path $param_data_path" 
    &$cmd $args | Out-Null
    
    Output: (The output contains logs of Python script along with Powershell error.)
    cmd.exe : 05/09/19 03:33:27 - INFO - Renaming the log file process started!
    At D:\Dinesh_Kumar\myproject\Invoke-Python.ps1:12 char:1
    + &$cmd $args | Out-Null
    + ~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (05/09/19 03:33:...rocess started!:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
     
    05/09/19 03:33:27 - INFO - Function rename_logs completed successfully!
    
    05/09/19 03:33:27 - INFO - Renaming the log file process ended!
    

    Thanks,
    Dinesh_Kumar
    Thursday, September 5, 2019 8:09 AM

Answers

All replies

  • Start-Process cmd.exe -ArgumentList '/c', $python_cmd, $pyscript_path, $param_data_path -NoNewWindow
    


    \_(ツ)_/

    Thursday, September 5, 2019 8:51 AM
  • Thanks for quick response. I tried with your above command. It skipped the errors now but the logs from Python script did not printed in the powershell console.

    I need the output of Python script.  Previously, it used to print the exit code of Python script.


    • Edited by Dinesh Edvin Thursday, September 5, 2019 10:31 AM
    Thursday, September 5, 2019 10:29 AM
  • You don't need to try so hard.

    Just specify the python command line. You don't need Start-Process or cmd.exe to run it.

    If you have variables, put them after the python command.


    D:\Models\ModelScripts\Production\python.cmd $pyscript_path $param_data_path



    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Dinesh Edvin Tuesday, September 17, 2019 11:46 AM
    Thursday, September 5, 2019 2:21 PM
    Moderator
  • I tried your way. But still getting the same error. I just need only the Python script logs have to be printed in the console.

    $param_data_path = 'D:\Dinesh_Kumar'
    $pyscript_path = 'D:\Dinesh_Kumar\log_rename.py'
    $python_cmd = 'D:\Models\ModelScripts\Production\python.cmd'
    
    D:\Models\ModelScripts\Production\python.cmd $pyscript_path $param_data_path

    Output:

    D:\Models\ModelScripts\Production\python.cmd : 06/09/19 03:31:15 - INFO - Renaming the log file process started!
    At D:\Dinesh_Kumar\myproject\Invoke-Python.ps1:9 char:1
    + D:\Models\ModelScripts\Production\python.cmd $pyscript_path $param_data_path
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (06/09/19 03:31:...rocess started!:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
     
    06/09/19 03:31:15 - INFO - Function rename_logs completed successfully!
    
    06/09/19 03:31:15 - INFO - Renaming the log file process ended!

    Friday, September 6, 2019 7:38 AM
  • Thanks for quick response. I tried with your above command. It skipped the errors now but the logs from Python script did not printed in the powershell console.

    I need the output of Python script.  Previously, it used to print the exit code of Python script.


    The CMD file is eating your output.  Run Python directly without a batch file.

    The exitcode in PS is $ERRORLEVEL.


    \_(ツ)_/

    Friday, September 6, 2019 7:47 AM
  • The syntax I gave you is correct. The error is a side-effect of the cmd.exe batch script you are running. You will need to fix that.



    -- Bill Stewart [Bill_Stewart]

    Friday, September 6, 2019 2:14 PM
    Moderator
  • Hi, yes the error is side effect of cmd.exe.

    I fixed that error, now its working fine. But its not capturing the exit code which returned by the Python script.

    I just need to capture the exit code and validate whether all the functionalities of Python script has been completed or not.

    Could you please suggest how to capture the exit code which is returned by the Python from Powershell.

    Thanks,

    Dinesh

    Monday, September 9, 2019 8:06 AM
  • This answer is easily accessible to you by doing a simple web search. Example:

    https://www.bing.com/search?q=powershell+get+exit+code+of+last+run+executable

    One of the results:

    https://www.itprotoday.com/powershell/running-executables-powershell

    That article contains a section titled "Getting an Executable's Exit Code".


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Dinesh Edvin Tuesday, September 17, 2019 11:46 AM
    Monday, September 9, 2019 2:09 PM
    Moderator