Skip to content
Advertisement

Run powershell in PHP with new-Object

I have to manage a cash register from PHP. If I run the following commands via cmd there are no problems. The cash register print the string perfectly.

I write the commands in this order:

JavaScript

The problem comes when I try to do this from php. I created a web server ( XAMPP ) to ensure that it can send commands from php to the serial port . I tried various ways of writing the script, but none of them work. Here’s an example of my .php

JavaScript

Everyone can help me? Thanks.

Advertisement

Answer

Each time you execute powershell.exe you’re passing only one line of the script to the completely new instance. That’s why it doesn’t work. You need to pass whole script to the single powershell instance. This can be done several ways:

Via StandradInput:

JavaScript

Via single-line argument for Command parameter:

JavaScript

Via Base64 string to EncodedCommand argument :

JavaScript

Or you can just drop temporary file to disk:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement