简体   繁体   English

使用php运行powershell脚本

[英]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. 我有一个文件,其中包含必须根据我提交给php表单的值运行的powershell脚本。

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. 我已经弄清楚了如何运行逐行命令,如果我可以将表单值作为输入传递给Shell脚本并获取输出,那将是很棒的。

Any ideas as to how I could post the form values to the powershell Read-host element? 关于如何将表单值发布到Powershell 读取主机元素的任何想法?

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 :) 编辑:如果有人可以让我知道一种通过php响应Read-Host的方法,那也很好:)

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. 在这种情况下,我不会使用Read-Host ,因为它仅对交互式脚本非常有用,在交互式脚本中,需要提示用户手动输入值。

As you want to call the script from php, using parameters would be better. 如要从php调用脚本,使用参数会更好。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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