【问题标题】:Run powershell script using php使用 php 运行 powershell 脚本
【发布时间】:2017-03-17 20:49:50
【问题描述】:

我有一个文件,其中包含必须根据我提交给 php 表单的值运行的 powershell 脚本。

我已经想出了如何运行逐行命令。如果我可以将表单值作为输入传递给 shell 脚本并获得输出,那就太好了。

关于如何将表单值发布到 powershell Read-host 元素的任何想法?

提前致谢:)

编辑:如果有人可以让我知道一种通过 php 响应 Read-Host 的方法,那也很棒:)

【问题讨论】:

  • 你能提供一些你迄今为止尝试过的例子吗?
  • 我还不知道如何将值输入到读取主机

标签: php powershell


【解决方案1】:

在这种情况下我不会使用Read-Host,因为它只对需要提示用户手动输入值的交互式脚本非常有用。

由于你想从 php 调用脚本,使用参数会更好。这样您就可以从命令行为您的脚本提供输入。

更新此示例脚本以使用参数...

$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"

运行时会产生如下输出

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

你会使用这样的参数:

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

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

运行时会产生如下输出

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

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

【讨论】:

  • 感谢负载。还有一种方法可以用来将输出的特定部分分配给 php 变量吗?
  • @Sandushi 最好问一个新问题,因为它与这个问题不同。
  • 你能检查这个链接stackoverflow.com/questions/42918185/…吗?
猜你喜欢
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 2021-10-13
  • 1970-01-01
  • 2014-10-11
  • 2017-05-26
  • 1970-01-01
  • 2018-08-12
相关资源
最近更新 更多