0

I have a PyInstaller compiled binary I am kicking off with a Batch script for ease of use (although I have tried a few different methods passing arguments directly via PowerShell etc) - the problem is that running the Batch via PowerShell leads to an issue where the Remote Host is unable to find a Network Share passed to the Python as an argument via the Batch file - but it works fine when the Batch file is executed locally.

Example PowerShell:

$Credential = Get-Credential
$session = New-PSSession -ComputerName "PCNAME" -Credential $Credential
Invoke-Command -Session $session -ScriptBlock { Invoke-Expression "C:\DIR\start.bat" }

(I know Invoke-Expression isn't great but I tried a few other methods as well and they all fail the same way - the Python Script errors out.)

Contents of start.bat

C:\DIR\someexe.exe -argument1 Blah -arg2 "\\SOMEPC\SomeShare\SomeDir"

When the PowerShell is executed, the batch runs and kicks off the Python binary which consistently throws an error - specifically, I am running a test for os.path.isdir / os.path.exists (tried both, same error) and it fails every time when executed remotely but works when the batch is executed locally.

What might be causing this issue? Pulling my hair out trying to understand why it works locally and not remotely.

(Also, yes, the directory does exist and the share is already mapped on the PC, as confirmed by it working locally and visually)

Relevant Python example from Argument Parsing

if args.arg2:
    dir = args.arg2[0]
    if os.path.exists(dir):
        print("Storing at custom directory: " + dir)
    else:
        print("Could not find directory: " + dir)
        sys.exit(1)

Error Received in PowerShell - Could not find directory: \\SOMEPC\SomeShare\SomeDir

Thanks...

panscan
  • 19
  • 5
  • 1
    1st why are you running PowerShell running CMD? 2nd you should share the complete error message you get. – Olaf Dec 29 '21 at 01:14
  • 1
    Hopefully the linked duplicate will help you resolve the issue. Let us know if you think your question isn't a duplicate. As for `Invoke-Expression`: indeed, it [should generally be avoided](https://stackoverflow.com/a/51252636/45375). In your case, use `& "C:\DIR\start.bat"` or - since quoting the path isn't strictly necessary here - just `C:\DIR\start.bat` – mklement0 Dec 29 '21 at 01:17
  • To clarify - this is a snippet of a much larger Powershell script designed to deploy and execute the compiled Python binary. The linked duplicate did indeed help me to resolve the issue - needed to map the drive remotely first, thank you. – panscan Dec 29 '21 at 02:57

0 Answers0