如何从PHP Linux服务器运行PowerShell脚本



我有一个应用程序,它会获取人员的最后登录详细信息,但由于AD中有多个域控制器,所以总是不准确。为了获取最新的最后登录信息,我们找到了一个PowerShell脚本,它将连接到两个域控制器并从两个域控制获取数据,并在比较后返回最近的记录(https://gallery.technet.microsoft.com/scriptcenter/Get-Last-Logon-for-Users-c8e3eab2)。我已将脚本放在下面。

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

现在的挑战是从PHP应用程序(Linux serever(调用这个脚本。我尝试了很多方法,但都无济于事。我已经放置了连接详细信息和属性(显示在应用程序AD详细信息中(,我们目前正在使用(也保存最后登录(。如果您能提供一种从PHP调用脚本的方法,请感谢您的帮助。

公共函数__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();
}

从PHP运行shell脚本总是会引发很多安全问题。

我建议您在PowerShell脚本中,将上次登录信息保存到数据库中,而不是使用PHP从数据库中检索。

例如,您可以使用以下内容将其保存到MySQL数据库中:

$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()

然后在PHP端,访问同一个数据库并获取信息。

最新更新