简体   繁体   中英

How to run a PowerShell script from PHP Linux server

I have an application which will fetch the lastlogon details of a staff which is not accurate always because of having Multiple Domain Controller in AD. To get the latest lastlogon, we found one PowerShell script which will connect to Two Domain controller and fetch the data from Two domain controller and return the recent record after comparing it ( https://gallery.technet.microsoft.com/scriptcenter/Get-Last-Logon-for-Users-c8e3eab2 ). I have placed the Script below.

PowerShell.ps1:

  Import-Module ActiveDirectory


  $hostname = "domaincontrollername1.test.**.**.***.**"

  Write-Host $hostname

  $user = Get-ADobject -Filter 'samaccountname -eq "id"' -SearchBase "OU=*** Users,DC=test,DC=***,DC=***,DC=***,DC=**" -Server $hostname -Properties "lastLogon"

  Write-Host $user

  $LastlogonTime1 = [DateTime]::FromFileTime($user.lastLogon)

  Write-Host $LastlogonTime1


  $hostname = "domaincontrollername2.test.**.**.***.**"

  Write-Host $hostname

  $user = Get-ADobject -Filter 'samaccountname -eq "id"' -SearchBase "OU=***Users,DC=***,DC=test,DC=***,DC=***,DC=**" -Server $hostname -Properties "lastLogon"

  Write-Host $user

  $LastlogonTime2 = [DateTime]::FromFileTime($user.lastLogon)

  Write-Host $LastlogonTime2


 if ($LastlogonTime1 -gt $LastlogonTime2)
 {​​​​​​​
    $Lastlogon = $LastlogonTime1
 }​​​​​​​
 else
 {​​​​​​​
    $Lastlogon = $LastlogonTime2
 }​​​​​​​


 Write-Host $Lastlogon

Now the challenge is to call this script from PHP application (Linux serever). I tried many ways with what I found in the internet but nothing helps. I have placed the connection details and the attributes(displaying in the application AD details) what we are using currently (holds the lastlogon too). Appreciate your help if you can provide a way to call the script from PHP.

public function __construct()

{

    $this->server     = "actual server";

    $this->port       = "number";

    $this->user       = "useraccount";

    $this->password   = "password";

    $this->dn         = "DC=***,DC=**,DC=**,DC=**,DC=***";

    $this->attributes = array("samaccountname","givenname","sn","name","title","description","department","lastlogon","memberof","userAccountControl");

    $this->filter     = "samaccountname=";

    parent::__construct();

}

Running shell scripts from PHP always raises lots of security issues.

I wold recommend you to, on your PowerShell script, save the last logon info to a database, than retrieve it from DB with PHP.

For example, you could save it to a MySQL DB with something like this:

$conn = ConnectToDatabase $user $pass $MySQLHost $database
$query = "INSERT INTO user (last_login) VALUES ('$LastLogon');"
$Command = New-Object MySql.Data.MySqlClient.MySqlCommand $query, $conn
$Command.ExecuteNonQuery()

Then on PHP side, access the same database and fetch the information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM