繁体   English   中英

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

[英]How to run a PowerShell script from PHP Linux server

我有一个应用程序,它将获取员工的最后登录详细信息,由于 AD 中有多个域 Controller,因此该信息并不准确。 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 )。 我已将脚本放在下面。

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 服务器)调用此脚本。 我用我在互联网上找到的东西尝试了很多方法,但没有任何帮助。 我已经放置了我们当前使用的连接详细信息和属性(显示在应用程序 AD 详细信息中)(也保存了 lastlogon)。 如果您能提供一种从 PHP 调用脚本的方法,感谢您的帮助。

公共 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();

}

从 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 端,访问相同的数据库并获取信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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