简体   繁体   English

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

[英]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.我有一个应用程序,它将获取员工的最后登录详细信息,由于 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 ). 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: 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).现在的挑战是从 PHP 应用程序(Linux 服务器)调用此脚本。 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).我已经放置了我们当前使用的连接详细信息和属性(显示在应用程序 AD 详细信息中)(也保存了 lastlogon)。 Appreciate your help if you can provide a way to call the script from PHP.如果您能提供一种从 PHP 调用脚本的方法,感谢您的帮助。

public function __construct()公共 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.从 PHP 运行 shell 脚本总是会引发很多安全问题。

I wold recommend you to, on your PowerShell script, save the last logon info to a database, than retrieve it from DB with PHP.我建议您在 PowerShell 脚本上将上次登录信息保存到数据库,而不是使用 PHP 从数据库中检索它。

For example, you could save it to a MySQL DB with something like this:例如,您可以将其保存到 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()

Then on PHP side, access the same database and fetch the information.然后在 PHP 端,访问相同的数据库并获取信息。

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

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