我有这个Powershell V2.0脚本,可以ping数据库服务器上的每台计算机,如果它成功ping,它将返回登录的用户名。现在,我的PHP显示计算机名称,但我需要执行Powershell脚本才能显示用户名。我怎样才能做到这一点?
工作小提琴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
?>
答案 0 :(得分:1)
您可以使用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
答案 1 :(得分:0)