PHP to Powershell Execution

Hello everyone. I'm having some troubles figuring out how to wrap my head around this project I'm working on. I want to have a webpage function as my script's gui.

Basically I want to have a simple PHP form that will pass the text box values to powershell variables.

Name :________
Email: _________

Write-Host $Name
Write-Host $Email

How would I go about doing that? I'm a novice to both PHP and powershell.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
On the PHP side of things, this article can point you to some good learning resources.

PHP has an introductory tutorial on dealing with HTML forms.  When you read it, follow the links for more details about how to work with forms safely.

I'll leave the PowerShell part to another expert.
Giovanni HewardCommented:
Here's a rough example:
if ($_REQUEST['name'] && $_REQUEST['email'] {
	if (preg_match('/(\w|\s|\d|\.|-|@)+/i', $_REQUEST['name'], $regs)) {
		$name = $regs[0];
		if (preg_match('/(\w|\s|\.|-)+/i', $_REQUEST['email'], $regs)) {
			$email = $regs[0];
			exec ( "echo .|powershell.exe someScript.ps1 -name \"$name\" -email \"$email\"", $output);

Open in new window

You'll definitely want to perform proper input validation to avoid possible injection vulnerabilities.

Your PowerShell script would then handle the input values (-name and -email) using the param section of the script.  More info here.

function foo()
  param([string]$name = "name", [string]$email = "email");
  Write-Host "Arg: $name";
  Write-Host "Arg: $email";

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Just a thought... If you're new to PHP, try to explain in writing (one sentence is all you need) what each line of code in the accepted solution is doing.  If you find that you can't do that, you might want to take some time to look up the functions, learn about regular expressions, etc.  If you're in an area of PHP programming where exec() seems like the answer, you're almost always asking the wrong question.  It's a place where many risks lie, and many hosting companies disable exec() for that reason.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.