将AD用户显示为HTML页面中的下拉列表

I am trying to display AD users as a dropdown in HTML page.

Following is the code I wrote in HTML to create dropdowns:

<h3>Search Account</h3>
<form>
   <legend>Select the username from below list</legend>
   <p>
   <label>Select list</label>
   <select id = "myList">
       <option value = "1">one</option>
       <option value = "2">two</option>
   </select>
   </p>
 </form>

Following is the code that I can use to get all Active AD usernames:

$users = Get-ADUser -Filter * -SearchBase "OU=User_Accounts,DC=test,DC=local" -Properties * | Where-Object {$_.UserAccountControl -eq 0x200} | Select-Object Name

foreach ($user in $users) {
    $userName = $user.Name
    Write-Host $userName
}

But, how can I integrate both codes, so that my HTML dropdown is dynamically filled with AD user names?

Any help will be appreciated.

you could try to use foreach in your html. if it is in php format then its better.

  <select id = "myList" name = "users">
     <?php foreach ($userName as $user): ?>
        <option value = "<?php echo $user['firstName'].' '.$user['lastName'];?>"><?php echo $user['name'];?></option>
     <?php endforeach ?>
  </select>

There are multiple ways to accomplish this. If you want to execute the PowerShell script and read the output with PHP you can to that with:

$output = shell_exec("powershell.exe path/to/myscript.ps1");

This requires that your script writes to the standard output stream. So you'll have to replace Write-Host with Write-Output. You can then iterate over the lines in $output to generate the dropdown.

But probably you don't want to query your AD every time when the page is loaded. If that is the case you might want to schedule the PowerShell script to run every hour or so and write the output to a textfile or database which is then read by PHP. If you want to export the users to a textfile:

$users = Get-ADUser -Filter * -SearchBase "OU=User_Accounts,DC=test,DC=local" -Properties * |
    Where-Object {$_.UserAccountControl -eq 0x200} |
    Select-Object Name |
    Out-File "path/to/file.txt" -Encoding UTF8

You can then read the textfile from PHP, reducing the page load time.