簡體   English   中英

如何在PHP中運行Powershell腳本?

[英]How can I run a Powershell script in PHP?

我有這個Powershell V2.0腳本,可以ping通數據庫服務器上的每台計算機,如果成功ping通,它將返回登錄的用戶名。 現在,使用我的PHP,我正在顯示計算機名稱,但是我需要執行Powershell腳本才能顯示用戶名。 我該如何實現?

工作小提琴在顯示計算機名稱的框中單擊

提前致謝!

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

或者,如果您想運行specfici 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

您可以使用execsystempassthru函數

暫無
暫無

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

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