Sandushi
Sandushi

Reputation: 3

Run powershell script using php

I have a file which contains a powershell script that has to be run depending on values i submit to a php form.

I have already figured out how to run linewise commands.It would be great if I could pass the form values as an input to the shell script and get the output.

Any ideas as to how I could post the form values to the powershell Read-host element?

Thanks in advance :)

Edit: If someone could please let me know a way to respond to Read-Host via php that would also be great :)

Upvotes: 0

Views: 323

Answers (1)

henrycarteruk
henrycarteruk

Reputation: 13227

I would not use Read-Host in this case as it's only really useful for an interactive script where the user needs to be prompted to manually input values.

As you want to call the script from php, using parameters would be better. This way you can provide input to your script from the command line.

Updating this example script to use parameters...

$computerName = Read-Host -Prompt "Enter your Computer Name"
$filePath = Read-Host -Prompt "Enter the File Path"

Write-Host "$computerName is your computer name!"
Write-Host "Your files are in $filePath location"

Which would generate the following output when run

PS C:\> Get-Something.ps1

Enter your Computer Name: SERVER1
Enter the File Path: C:\folder
SERVER1 is your computer name!
Your files are in C:\folder location

You would use parameters like this:

Param(
    [string]$computerName,
    [string]$filePath
)

Write-Host "$computerName is your computer name!"
Write-Host "Your files are in $filePath location"

Which would generate the following output when run

PS C:\> Get-Something.ps1 –computerName SERVER1 –filePath C:\folder

SERVER1 is your computer name!
Your files are in C:\folder location

Upvotes: 1

Related Questions