2

I have this Powershell V2.0 script that pings every computer on the database server and if it pings succesfully it will return the usernames that are logged on. Now, with my PHP I am displaying the computer names but I need to execute the Powershell script in order to display the usernames. How Can I achieve this?

Working fiddle click on the box it displays the computer name

thanks in advance!

Powershell script:

#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

?>
1

2 Answers 2

2

You can use exec() or shell_exec() to run Windows commands.

If you need to use powershell, simply start powershell before running your command:

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

Or if you want to run a specfici powershell script:

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

If you need to bypass the execution policy that denies you running powershell scripts:

$output = shell_exec('powershell -ExecutionPolicy ByPass -File C:\Path\To\Script.ps1');
echo $output // True
Sign up to request clarification or add additional context in comments.

5 Comments

I get not output? I wrote then entire path where my powershell script is located
Test-Path C:\\ is an example Powershell script, that's where you would place your actual Powershell script.
this is the PHP line I have, Its in my while loop: $output = shell_exec('\\RETCA230-NT4004\\maesc4_php\draft_user.ps1'); 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>User:' . $output . '<br>Pod:' . $pod . '</p></div>' . "\n"; As you can see, I'm trying to output the usernames that are logged on for each computer. I'm sure im doing this wrong, or am I not properly returning the usernames in my powershell script?
Please see my update, hopefully with that information you should be able to get what you need.
I actually changed mind, I'll be storing the values retrieved from my powershell script to write on a database table, its more efficient that way, thanks for your help though!
0

You can use either exec, system or passthru function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.