Calling python with Powershell from C# works on some servers not others

Lou's World Inc 41 Reputation points
2022-03-23T05:48:13.79+00:00

We have an API app written in C#. The app instantiates Powershell then makes a "Commands.AddScript" call to insert a script which calls python scripts. This process works fine in a local environment and on one Azure Windows VM but we are unable to replicate this process on other Azure Windows VMs. Here is the test code.

Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
PowerShell ps = PowerShell.Create();

var fileCopy = "This-will-be written to the text file" ;
ps.Commands.AddScript(@" """ + fileCopy + @""" | Out-File -FilePath " + cloneScriptLocation + @"c:\output_location\powershell-output.txt");//This works in local environment and all Azure VMs


ps.Commands.AddScript(@"cd  c:\output_location" );
ps.Commands.AddScript(@"python .\python-output.py " );//This works in local environment plus one Azure VM but does not get called on other Azure VMs

ps.Invoke();
runspace.Close();

The powershell "Out-File"command is called in all environments but the python command is not. On the Azure VM where the python command is not called the same code can be successfully called from the Azure Portal or on the VM directly using cmd or powershell.

I'm seeing no errors. The directory permissions appear similar on all VMs

What could be preventing python from being called in only certain environments?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,281 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,407 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 32,246 Reputation points
    2022-03-23T22:43:50.817+00:00

    Sounds like you are missing a folder in the system path, and possibly the PS execution policy is not set.

    On my Win11 laptop, I found Python.exe at C:\Users\Me\AppData\Local\Microsoft\WindowsApps\python.exe
    The WindowsApps folder is listed in the path. You might have a similar problem. You may need to change the order of folders in the Path.

    Have your PS script create a transcript and test for the programs. Review the MyScript.txt file.

    Start-Transcript -Path 'C:\Logs\MyScript.txt' 
    ''
    'Here are the folders in the system path.'
    ''
    ($env:path).split(';')
    ''
    'Look for Powershell and python via the path.'
    ''
    where.exe powershell
    where.exe python 
    ''
    'Look for python.exe'
    Get-ChildItem c:\ -Filter python.exe -Recurse -ErrorAction SilentlyContinue
    Stop-Transcript 
    
    1 person found this answer helpful.