安裝PowerShell - Active Directory Module解決AD大小事

安裝 PowerShell - Active Directory Module 解決 AD 大小事

在企業內,經常需要查詢 Active Directory(AD) 資訊,有幾種方式(方向)可以來執行。

  1. 使用 AD 工具
  2. 撰寫 C# 程式
  3. PowerShell - Active Directory Module

使用 AD 工具

如果你是 IT 人員,應該會選擇使用 Active Directory 工具,網路上的 Active Directory 工具很多,例如,AD Explorer - Windows Sysinternals 就是個很老牌又好用的工具。

寫 C# 程式

如果你是個 .NET 開發人員,可以參考 System.DirectoryServices Namespace 進行開發,例如,我想取得 Active Directory 內全部使用者:

using (DirectoryEntry entry = new DirectoryEntry("root", "username", "password", AuthenticationTypes.Secure))
{
 using (DirectorySearcher search = new DirectorySearcher(entry))
 {
  foreach (SearchResult searchResult in search.FindAll())
  {
   foreach (string key in searchResult.Properties.PropertyNames)
   {
    foreach (var val in searchResult.Properties[key])
    {
     Console.WriteLine($"The properties of the searchResult are: {val}");
    }
   }
  }
 }
}

"root" 有一定格式。

如果我只想查詢某個使用者呢?這部份除非要整合應用程式或練功,不然實在太麻煩了。

PowerShell - Active Directory Module

這介於 IT 與 Dev 之間的選擇,暫定 Ops 好了。其實 PowerShell 一直都有提供一個 Active Directory Module,Active Directory Module 裡面已經整合好許多好用的 Cmdlet,能對 Active Directory 進行完整的操作。(異動部分權限夠的話)

Windows 8, 8.1, 10

在 Windows 8、Windows 8.1、Windows 10 需要額外安裝 Remote Server Administration Tool。安裝完成之後不需重開機,Active Directory Module 裡的 Cmdlet 在被使用時會自動被 PowerShell 載入。

Windows Server - RSAT-AD-PowerShell

在 Windows Server 安裝,談 PowerShell 當然用 PowerShell 來安裝:

PS C:\>Get-WindowsFeature -Name RSAT-AD*

Display Name                                            Name                       Install State
------------                                            ----                       -------------
        [ ] AD DS and AD LDS Tools                      RSAT-AD-Tools                  Available
            [ ] Active Directory module for Windows ... RSAT-AD-PowerShell             Available
            [ ] AD DS Tools                             RSAT-ADDS                      Available
                [ ] Active Directory Administrative ... RSAT-AD-AdminCenter            Available
                [ ] AD DS Snap-Ins and Command-Line ... RSAT-ADDS-Tools                Available
            [ ] AD LDS Snap-Ins and Command-Line Tools  RSAT-ADLDS                     Available


PS C:\>Add-WindowsFeature -Name RSAT-AD-PowerShell

Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {Active Directory module for Windows Power...


PS C:\> Get-WindowsFeature -Name RSAT-AD*

Display Name                                            Name                       Install State
------------                                            ----                       -------------
        [X] AD DS and AD LDS Tools                      RSAT-AD-Tools                  Installed
            [X] Active Directory module for Windows ... RSAT-AD-PowerShell             Installed
            [ ] AD DS Tools                             RSAT-ADDS                      Available
                [ ] Active Directory Administrative ... RSAT-AD-AdminCenter            Available
                [ ] AD DS Snap-Ins and Command-Line ... RSAT-ADDS-Tools                Available
            [ ] AD LDS Snap-Ins and Command-Line Tools  RSAT-ADLDS                     Available

如果要用【程式和功能】來啟用,請參上面資訊階層找到「AD DS and AD LDS Tools」啟用「Active Directory module for Windows PowerShell」即可。

接下來,你就可以參考 Active Directory module for Windows PowerShell 文件中 Get-AD* Cmdlet。

例如,查詢某個 AD 叫 Bruce 的使用者的資料:

PS C:\>Get-ADUser brucechen


DistinguishedName : CN=BruceChen,...
Enabled           : True
GivenName         : Bruce
Name              : BruceChen
ObjectClass       : user
ObjectGUID        : eea50533-...
SamAccountName    : BruceChen
SID               : S-1-5-21-...
Surname           : Chen
UserPrincipalName : ...@kkbruce.tw

例如,查詢 Active Directory domain 的資訊,可以用 Get-ADDomain 或 Get-ADDomainController。又例如,查詢已加入 Active Directory ,名稱為 mis 開頭的電腦資訊,可以這樣下:

Get-ADComputer -Filter 'Name -like "mis*"' -Properties IPv4Address | FT Name,DNSHostName,IPv4Address -A

你能發現,就查詢 Active Directory 資訊而言,Active Directory module 的 Get-AD* Cmdlet 實在太好用了。當然,如果有你有足夠的權限,那麼其他異動類 Cmdlet 也是非常的給力的。

沒有留言:

張貼留言

感謝您的留言,如果我的文章你喜歡或對你有幫助,按個「讚」或「分享」它,我會很高興的。