簡體   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