gpt4 book ai didi

php - 如何在 PHP 中运行 Powershell 脚本?

转载 作者:行者123 更新时间:2023-11-29 00:08:13 28 4
gpt4 key购买 nike

我有这个 Powershell V2.0 脚本,它可以 ping 数据库服务器上的每台计算机,如果 ping 成功,它将返回登录的用户名。现在,我使用 PHP 显示计算机名称,但我需要执行 Powershell 脚本才能显示用户名。我怎样才能做到这一点?

工作 fiddle click on the box it displays the computer name

提前致谢!

Powershell 脚本:

#call procedure to execute the command
$command.CommandText = "SELECT w.ws FROM
sandbox_maesc4.coordinates c
INNER JOIN
softphone_materials_updater.workstations w
ON c.station_number = w.pod";

#Execute the Command by reading each row
$reader = $command.ExecuteReader();
#Read values from database
$workstation_list = $( while ($reader.Read() ){
$reader.GetValue(0)
})
#close reader
$reader.Close()

#ping each computer if online, obtain information about the local hard drives only
try{
foreach($pc_db in $workstation_list){
if(Test-Connection -ComputerName $pc_db -Count 1 -ErrorAction SilentlyContinue){
$username = Get-WMIObject -ComputerName $pc_db -Class Win32_ComputerSystem | Select-Object Username #-ExpandProperty
#Get-WMIObject -ComputerName $pc_db -Class Win32_ComputerSystem -Property Username
Write-host "$username is logged into $pc_db";
} #end if

else{
Write-Host "$pc_db is Offline" -ForegroundColor Red
}#end else

}#end for each loop
}#end try
catch{
Write-Host "Caught the exception";
Write-Host "$_";
throw $error[0].Exception
}#end catch
$connection.Close()
}#end ping_test function

ping_getUser #execute function

PHP:

<?
//query to show workstation/desks information from DB for the DESKS
$station_sql = "SELECT coordinate_id, section_name, x_coord, y_coord, w.ws, w.pod FROM
sandbox_maesc4.coordinates c
INNER JOIN
softphone_materials_updater.workstations w
ON c.station_number = w.pod";


$station_result = mysqli_query($conn,$station_sql);

//see if query is good
if($station_result === false) {
die(mysqli_error());
}

//display hidden DIV when toggle with information inside
while($row = mysqli_fetch_assoc($station_result)){
//naming values

//coordinates table
$id = $row['coordinate_id'];
$x_pos = $row['x_coord'];
$y_pos = $row['y_coord'];
$sec_name = $row['section_name'];

//workstations table
$workstations = $row['ws'];
$pod = $row['pod'];

//display DIV with the content inside
echo '<div class="station_info_" id="station_info_' . $id . '" style="left:' . $x_pos . 'px;top:' . $y_pos . 'px;"><p class="numb">Section:' . $sec_name . '<br>Station:' . $workstations . '<br>Pod:' . $pod . '</p></div>' . "\n";
}//end while loop for station_result

?>

最佳答案

您可以使用 exec()shell_exec()运行 Windows 命令。

如果您需要使用 powershell,只需在运行命令之前启动 powershell:

$output = shell_exec('powershell Test-Path C:\\');
echo $output // True

或者如果你想运行一个特定的 powershell 脚本:

$output = shell_exec('powershell -File C:\Path\To\Script.ps1');
echo $output // True

如果您需要绕过拒绝您运行 powershell 脚本的执行策略:

$output = shell_exec('powershell -ExecutionPolicy ByPass -File C:\Path\To\Script.ps1');
echo $output // True

关于php - 如何在 PHP 中运行 Powershell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26509661/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com