PowerShell 2.0 實踐(七)管理活動目錄(上)
上一次我們對Windows日志進行了深入的研究與測試,重在設置不同的條件來查詢我們感興趣的日志,精確的條件可以快速找到問題的癥狀,方便管理員及時排錯。本次我們來重點關注一下Windows Server,以Windows Server 2008 R2 DataCenter為例,對活動目錄進行一些測試。對于活動目錄,計劃寫三篇博文:上中下,本次為第一篇,主要來測試域用戶的編輯等。
本系列所有腳本均在Windows Server 2008 R2 DataCenter (PowerShell 2.0) + PowerGUI Script Editor Free Edition x64中測試通過。
活動目錄(Active Directory)是Windows操作系統下的一種企業資源解決方案。其體系結構如下:
在Windows Server 2008 R2上安裝了活動目錄角色后,會有如下一些管理工具:
Active Directory Administrator Center是一個很有用的工具:
界面全部用WPF設計:
- 管理用戶賬戶
對于活動目錄來說,用戶、組的管理是一個核心內容。在PowerShell 2.0中,可以使用如下方法管理用戶、組:
ADSI(Active Directory Service Interfaces,活動目錄服務接口):WinNT Provider、LDAP Provider
System.DirectoryServices
System.DirectoryServices.AccountManagement
Microsoft Active Directory Cmdlets(微軟提供的PowerShell擴展)
Quest Active Directory Comlets(Quest公司提供的PowerShell擴展)
在創建域用戶、域組之前,先創建一些本地用戶和組。
2、使用.NET類創建一個本地用戶:
#創建一個本地用戶
Clear-Host
[void][reflection.assembly]::Load("System.DirectoryServices.AccountManagement, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
#PowerShell 2.0中還可以這樣添加程序集
#Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$password = Read-Host "Password" -AsSecureString #作為密碼
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "userid", $password
$ctype = [System.DirectoryServices.AccountManagement.ContextType]::Machine #本地用戶
$context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, "BrooksPCNB" #替換為你自己的機器名
$usr = New-Object -TypeName System.DirectoryServices.AccountManagement.UserPrincipal -ArgumentList $context
$usr.SamAccountName = "Newuser1"
$usr.SetPassword($cred.GetNetworkCredential().Password)
$usr.DisplayName = "New User"
$usr.Enabled = $true
$usr.ExpirePasswordNow() #下次登錄時必須修改密碼
$usr.Save()
首先會提示輸入待創建用戶的密碼,注意密碼強度要符合策略,運行結果:
3、創建本地組:
#創建一個本地組
Clear-Host
[void][reflection.assembly]::Load("System.DirectoryServices.AccountManagement, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
$ctype = [System.DirectoryServices.AccountManagement.ContextType]::Machine
$context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, "BrooksPCNB"
$gtype = [System.DirectoryServices.AccountManagement.GroupScope]::Local
$grp = New-Object -TypeName System.DirectoryServices.AccountManagement.GroupPrincipal -ArgumentList $context, "Group1"
$grp.IsSecurityGroup = $true
$grp.GroupScope = $gtype
$grp.Save()
運行結果:
4、將用戶添加進組:
Clear-Host
[void][reflection.assembly]::Load("System.DirectoryServices.AccountManagement, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
$ctype = [System.DirectoryServices.AccountManagement.ContextType]::Machine
$context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, "BrooksPCNB"
$idtype = [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName
$grp = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, $idtype, "Group1")
$grp.Members.Add($context, $idtype, "Newuser1")
$grp.Save()
運行結果:
5、使用Microsoft Active Directory Cmdlets創建域用戶:
New-ADUser -Name "Brooks" -SamAccountName "Brooks" `
-GivenName "Brooks" -Surname "Brooks" `
-Path "OU=Domain Controllers,DC=Lucifer,DC=com" -DisplayName "Mr.Brooks" `
-AccountPassword (Read-Host -AsSecureString "AccountPassword") -CannotChangePassword $false `
-ChangePasswordAtLogon $true -UserPrincipalName Brooks@Brooks.com
運行結果:
6、使用Quest Active Directory Cmdlets創建域用戶:
若你還沒有添加Snapin,則需要先添加:
Add-PSSnapin Quest.ActiveRoles.ADManagement
New-QADUser -Name "Brooks" -FirstName "Mr" -LastName "Brooks" `
-DisplayName "Brooks" -SamAccountName "Brooks" `
-UserPassword (Read-Host -AsSecureString) -UserPrincipalName "Brooks@Lucifer.org" `
-ParentContainer "OU=Domain Controllers,DC=Lucifer,DC=com"
運行結果:
7、關于ADSI適配器的WinNT和LDAP Provider,其語法我不太熟悉,故不再舉例,感興趣的朋友可以查查相關資料。
不過ADSI有個注意點,ADSI適配器直接操作本地SAM(Security Accounts Manage,安全賬戶管理器數據庫),其位于:%windir%\System32\config目錄,包括兩個文件 SAM、SECURITY:
SAM本地數據庫存儲了如下重要信息:
用戶賬戶信息
組信息
計算機賬戶信息(僅適用于WindowsNT、Workstation)
受信任的賬戶關聯
同時SAM本地數據庫是注冊表的一部分,其位于如下分支:
HKEY_LOCAL_MACHINE\SAM
HKEY_LOCAL_MACHINE\SECURITY
SAM本地數據庫不能直接編輯,進入操作系統后即被鎖定。可以通過第三方工具如saminside進行破解,注意,僅限于找回丟失的密碼,不要嘗試盜取別人密碼等操作。
小結:
本次我們練習了使用.NET框架中的類以及微軟與Quest的PowerShell擴展來創建域用戶、組,部署時需要注意System.DirectoryServices
System.DirectoryServices.AccountManagement
需要.NET Framework 3.5或更新版本。
通過微軟和Quest的PowerShell擴展可以很簡潔的創建域用戶、組,推薦使用這兩種命令,要確保密碼強度符合組策略。最后我們熟悉了下SAM數據庫,SAM存儲了很多用戶賬戶信息,非常重要,理解SAM數據庫可以更好地理解Active Directory體系結構。下一次我們將練習批量創建域用戶、組,以及其他一些更高級的操作。

浙公網安備 33010602011771號