簡體   English   中英

使用php運行powershell腳本

[英]Run powershell script using php

我有一個文件,其中包含必須根據我提交給php表單的值運行的powershell腳本。

我已經弄清楚了如何運行逐行命令,如果我可以將表單值作為輸入傳遞給Shell腳本並獲取輸出,那將是很棒的。

關於如何將表單值發布到Powershell 讀取主機元素的任何想法?

提前致謝 :)

編輯:如果有人可以讓我知道一種通過php響應Read-Host的方法,那也很好:)

在這種情況下,我不會使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM