简体   繁体   中英

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 :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM