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();
}