y1w3n's blog

Windows AD for skill54

2026-07-14

全國技能競賽 網路安全
note by y1w3n

note by AI

Claude 版本流程整理

階段 1:AD DS 核心觀念與建置

1-1 安裝 AD DS 角色並晉升第一台 DC

1
2
3
4
5
6
7
8
9
Install-WindowsFeature AD-Domain-Services -IncludeManagementTools

Install-ADDSForest `
-DomainName "contoso.local" `
-DomainNetbiosName "CONTOSO" `
-ForestMode "WinThreshold" `
-DomainMode "WinThreshold" `
-InstallDns:$true `
-SafeModeAdministratorPassword (ConvertTo-SecureString "P@ssw0rd123!" -AsPlainText -Force)

完成後自動重開機,重開後用 CONTOSO\Administrator 登入驗證。

1-2 驗證安裝結果

1
2
3
Get-ADDomain
Get-ADForest
dcdiag /v

1-3 建立第二台 DC(複寫驗證)

在 DC2 上先加入網域,再執行:

1
2
3
4
5
6
7
Install-WindowsFeature AD-Domain-Services -IncludeManagementTools

Install-ADDSDomainController `
-DomainName "contoso.local" `
-InstallDns:$true `
-Credential (Get-Credential CONTOSO\Administrator) `
-SafeModeAdministratorPassword (ConvertTo-SecureString "P@ssw0rd123!" -AsPlainText -Force)

驗證複寫:

1
2
repadmin /replsummary
repadmin /showrepl

1-4 建立子網域(Child Domain)

在新 VM 上:

1
2
3
4
5
6
7
Install-ADDSDomainController `
-NewDomainName "child" `
-ParentDomainName "contoso.local" `
-DomainType "ChildDomain" `
-InstallDns:$true `
-Credential (Get-Credential CONTOSO\Administrator) `
-SafeModeAdministratorPassword (ConvertTo-SecureString "P@ssw0rd123!" -AsPlainText -Force)

1-5 FSMO 角色查詢與轉移

查詢:

1
netdom query fsmo

轉移(在目標 DC 上執行):

1
2
Move-ADDirectoryServerOperationMasterRole -Identity "DC2" `
-OperationMasterRole SchemaMaster,DomainNamingMaster,PDCEmulator,RIDMaster,InfrastructureMaster

若原 DC 已離線,強制奪取(Seize):

1
2
3
4
5
6
7
8
ntdsutil
roles
connections
connect to server DC2
quit
seize pdc
quit
quit

1-6 Sites and Services

1
2
New-ADReplicationSite -Name "Taipei-Site"
New-ADReplicationSubnet -Name "192.168.10.0/24" -Site "Taipei-Site"

GUI 路徑:Active Directory Sites and Services → 右鍵 Sites → New Site → 設定 Subnet 與站台連結(Site Link)複寫排程(預設 IntersiteReplicationInterval 180 分鐘可依賽題調整)。


階段 2:使用者與群組管理實作

2-1 建立 OU

1
2
New-ADOrganizationalUnit -Name "IT部門" -Path "DC=contoso,DC=local"
New-ADOrganizationalUnit -Name "業務部門" -Path "DC=contoso,DC=local"

2-2 CSV 批次建立使用者

CSV 範例(users.csv):

1
2
Name,SamAccountName,OU,Password
王小明,wang,OU=IT部門,DC=contoso,DC=local,P@ssw0rd1!

匯入腳本:

1
2
3
4
5
6
7
8
Import-Csv "C:\users.csv" | ForEach-Object {
New-ADUser -Name $_.Name `
-SamAccountName $_.SamAccountName `
-Path $_.OU `
-AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -Force) `
-Enabled $true `
-ChangePasswordAtLogon $true
}

2-3 批次修改屬性

1
Get-ADUser -Filter {Department -eq "IT"} | Set-ADUser -Office "台北總部"

2-4 委派控制

GUI:右鍵 OU → Delegate Control → 選擇使用者/群組 → 勾選要委派的任務(如重設密碼、建立/刪除使用者)

PowerShell 對應(需 dsacls):

1
dsacls "OU=IT部門,DC=contoso,DC=local" /I:S /G "CONTOSO\HelpDeskGroup:CA;Reset Password;user"

2-5 群組建立與巢狀(AGDLP)

1
2
3
New-ADGroup -Name "G_IT_Global" -GroupScope Global -GroupCategory Security -Path "OU=IT部門,DC=contoso,DC=local"
New-ADGroup -Name "DL_Printer_Access" -GroupScope DomainLocal -GroupCategory Security -Path "OU=IT部門,DC=contoso,DC=local"
Add-ADGroupMember -Identity "DL_Printer_Access" -Members "G_IT_Global"

2-6 細緻密碼原則(PSO)

1
2
3
4
5
6
7
8
9
New-ADFineGrainedPasswordPolicy -Name "IT-PSO" `
-Precedence 10 `
-MinPasswordLength 12 `
-PasswordHistoryCount 5 `
-LockoutThreshold 3 `
-ComplexityEnabled $true `
-ReversibleEncryptionEnabled $false

Add-ADFineGrainedPasswordPolicySubject -Identity "IT-PSO" -Subjects "G_IT_Global"

階段 3:GPO(群組原則)

3-1 建立與連結 GPO

1
New-GPO -Name "IT部門限制原則" | New-GPLink -Target "OU=IT部門,DC=contoso,DC=local"

3-2 編輯 GPO(GUI)

Group Policy Management → 右鍵 GPO → Edit → 路徑範例:

  • 登入腳本:User Configuration → Policies → Windows Settings → Scripts (Logon/Logoff)
  • 磁碟機對應:User Configuration → Preferences → Windows Settings → Drive Maps
  • 資料夾重新導向:User Configuration → Policies → Windows Settings → Folder Redirection

3-3 安全性篩選(限制套用對象)

1
2
Set-GPPermission -Name "IT部門限制原則" -TargetName "Authenticated Users" -TargetType Group -PermissionLevel None
Set-GPPermission -Name "IT部門限制原則" -TargetName "G_IT_Global" -TargetType Group -PermissionLevel GpoApply

3-4 軟體派送

GPO Edit → Computer Configuration → Policies → Software Settings → Software Installation → New → Package(需 MSI 檔置於共用資料夾,UNC 路徑)

3-5 強制與封鎖繼承

1
2
Set-GPLink -Target "OU=IT部門,DC=contoso,DC=local" -Name "IT部門限制原則" -Enforced Yes
Set-GPInheritance -Target "OU=業務部門,DC=contoso,DC=local" -IsBlocked Yes

3-6 疑難排解

1
2
3
gpupdate /force
gpresult /r
gpresult /h report.html

GUI:Group Policy Management → 右鍵 OU → Group Policy Modeling(模擬)/ Group Policy Results(實際套用結果)

3-7 WMI 篩選器

GPO Edit 左側 WMI Filters → New → 範例(僅套用於 Windows 10 以上):

1
SELECT * FROM Win32_OperatingSystem WHERE Version >= "10.0"

階段 4:DNS 整合

4-1 建立正向/反向區域

1
2
Add-DnsServerPrimaryZone -Name "contoso.local" -ReplicationScope "Forest"
Add-DnsServerPrimaryZone -NetworkID "192.168.10.0/24" -ReplicationScope "Forest"

4-2 條件式轉寄站

1
Add-DnsServerConditionalForwarderZone -Name "partner.com" -MasterServers 10.0.0.5

4-3 老化清除設定

1
2
Set-DnsServerScavenging -ScavengingState $true -ScavengingInterval 7.00:00:00
Set-DnsServerZoneAging -Name "contoso.local" -Aging $true

4-4 疑難排解

1
2
nslookup dc1.contoso.local
dcdiag /test:dns /v

階段 5:DHCP 整合

5-1 安裝並授權

1
2
Install-WindowsFeature DHCP -IncludeManagementTools
Add-DhcpServerInDC -DnsName "dc1.contoso.local" -IPAddress 192.168.10.10

5-2 建立範圍

1
2
Add-DhcpServerv4Scope -Name "IT-Scope" -StartRange 192.168.10.100 -EndRange 192.168.10.200 -SubnetMask 255.255.255.0
Set-DhcpServerv4OptionValue -ScopeId 192.168.10.0 -DnsServer 192.168.10.10 -DnsDomain "contoso.local" -Router 192.168.10.1

5-3 保留與 Failover

1
2
3
4
5
6
Add-DhcpServerv4Reservation -ScopeId 192.168.10.0 -IPAddress 192.168.10.150 -ClientId "AA-BB-CC-DD-EE-FF"

Add-DhcpServerv4Failover -Name "DHCP-Failover" `
-PartnerServer "dc2.contoso.local" `
-ScopeId 192.168.10.0 `
-LoadBalancePercent 50

5-4 安全動態更新(搭配 DNS)

在 DHCP 範圍 → DNS 標籤 → 勾選「僅在 DHCP 用戶端要求時動態更新」+「僅對安全動態更新使用認證」


階段 6:進階目錄服務

6-1 建立信任關係

1
netdom trust contoso.local /d:partner.local /add /twoway /UserD:Administrator /PasswordD:*

6-2 AD 回收筒啟用與還原

1
2
3
4
Enable-ADOptionalFeature "Recycle Bin Feature" -Scope ForestOrConfigurationSet -Target "contoso.local"

# 誤刪還原
Get-ADObject -Filter {displayName -eq "王小明"} -IncludeDeletedObjects | Restore-ADObject

6-3 RODC 部署

1
2
3
4
5
6
7
8
Install-ADDSDomainController `
-DomainName "contoso.local" `
-ReadOnlyReplica:$true `
-SiteName "Taipei-Site" `
-InstallDns:$true `
-DelegatedAdministratorAccountName "CONTOSO\HelpDeskAdmin" `
-Credential (Get-Credential CONTOSO\Administrator) `
-SafeModeAdministratorPassword (ConvertTo-SecureString "P@ssw0rd123!" -AsPlainText -Force)

6-4 密碼複寫原則(PRP)

1
2
Add-ADDomainControllerPasswordReplicationPolicy -Identity "RODC1" -AllowedList "G_IT_Global"
Add-ADDomainControllerPasswordReplicationPolicy -Identity "RODC1" -DeniedList "Domain Admins"

6-5 SYSVOL 複寫狀態確認(DFSR)

1
2
dfsrmig /getmigrationstate
repadmin /showrepl DC1 DC=DomainDnsZones,DC=contoso,DC=local

階段 7:憑證與驗證服務

7-1 安裝企業 CA

1
2
3
4
5
6
7
Install-WindowsFeature AD-Certificate -IncludeManagementTools
Install-AdcsCertificationAuthority `
-CAType EnterpriseRootCA `
-CryptoProviderName "RSA#Microsoft Software Key Storage Provider" `
-KeyLength 2048 `
-HashAlgorithmName SHA256 `
-ValidityPeriod Years -ValidityPeriodUnits 5

7-2 憑證範本與自動註冊

GUI:certtmpl.msc → 複製範本(如 User)→ 設定屬性 → 發佈於 CA(certsrv.msc → 憑證範本 → 新增要發行的範本)

GPO 設定自動註冊:Computer/User Configuration → Policies → Windows Settings → Security Settings → Public Key Policies → Certificate Services Client - Auto-Enrollment → Enabled

7-3 NPS(RADIUS / 802.1X)

1
Install-WindowsFeature NPAS -IncludeManagementTools

GUI:nps.msc → RADIUS Clients and Servers → New RADIUS Client(輸入交換器 IP + 共用密鑰)→ Policies → Network Policies → New(設定條件如群組成員 + 驗證方法 PEAP)


階段 8:備份、還原與災難復原

8-1 System State 備份

1
2
Install-WindowsFeature Windows-Server-Backup
wbadmin start systemstatebackup -backupTarget:E: -quiet

8-2 進入 DSRM

1
2
bcdedit /set safeboot dsrepair
shutdown /r /t 0

(還原正常開機記得執行 bcdedit /deletevalue safeboot

8-3 授權還原(Authoritative Restore)

1
2
3
4
5
6
ntdsutil
activate instance ntds
authoritative restore
restore subtree "OU=IT部門,DC=contoso,DC=local"
quit
quit

配合 wbadmin start systemstaterecovery -version:<版本> -backupTarget:E: 先還原再執行授權還原。


階段 9:安全性強化

9-1 LAPS 部署

1
2
3
Install-Module AdmPwd.PS
Update-AdmPwdADSchema
Set-AdmPwdComputerSelfPermission -OrgUnit "OU=Clients,DC=contoso,DC=local"

GPO:安裝 LAPS GPO 範本後設定 Computer Configuration → Policies → Administrative Templates → LAPS → 啟用密碼管理

9-2 進階安全性稽核

GPO:Computer Configuration → Policies → Windows Settings → Security Settings → Advanced Audit Policy Configuration → 啟用「帳戶登入」「目錄服務存取」等子類別

1
auditpol /set /subcategory:"Directory Service Access" /success:enable /failure:enable

9-3 安全性基準套用

下載 Microsoft Security Compliance Toolkit → 使用 LGPO.exe 匯入基準 GPO 至 SYSVOL,或用 GPMC 匯入既有 GPO 備份。


階段 10:PowerShell 自動化

10-1 常用指令速查

1
2
3
4
5
Get-ADUser -Filter * -Properties * | Select Name,SamAccountName,Enabled
Get-ADGroupMember -Identity "G_IT_Global"
Get-ADComputer -Filter * | Select Name,OperatingSystem
Search-ADAccount -LockedOut
Search-ADAccount -PasswordExpired

10-2 批次匯出稽核報表

1
2
3
Get-ADUser -Filter * -Properties LastLogonDate,PasswordLastSet |
Select Name,SamAccountName,LastLogonDate,PasswordLastSet |
Export-Csv "C:\report.csv" -NoTypeInformation -Encoding UTF8

10-3 批次建立 OU + 群組(結構化腳本範例)

1
2
3
4
5
$depts = @("業務部","研發部","財務部")
foreach ($d in $depts) {
New-ADOrganizationalUnit -Name $d -Path "DC=contoso,DC=local"
New-ADGroup -Name "G_$d" -GroupScope Global -GroupCategory Security -Path "OU=$d,DC=contoso,DC=local"
}

階段 11:疑難排解工具熟練

11-1 dcdiag / repadmin 常用組合

1
2
3
4
dcdiag /v /c /d /e /s:DC1
repadmin /replsummary
repadmin /showrepl DC1
repadmin /syncall /AdeP

11-2 ADSI Edit(查看/修改原始屬性)

1
adsiedit.msc

連接到 Default naming context → 展開找到物件 → 右鍵 Properties 直接修改 LDAP 屬性(如 msDS-* 屬性)

11-3 ldp.exe(LDAP 查詢)

1
ldp.exe

Connection → Connect(輸入 DC IP)→ Bind → View → Tree → 展開 DN 逐層檢查

11-4 事件記錄檢查重點

eventvwr.msc → Applications and Services Logs → Directory Service / DNS Server;System log 篩選 Source: NETLOGON, Kerberos


前情提要

我現在從官方那邊拿到 Windows Server 2025 iso,電腦改名為 DC01 了,然後設了固定 IP(IP: 192.168.19.10、Subnet mask: 255.255.255.0、Gateway: 192.168.19.2、DNS: 127.0.0.1)。現在裝ㄌ AD DS 角色跟升級成網域控制站,Add a new forest 叫 yi.local(NetBIOS: YI),DSRM 密碼設定完成,然後要開始做 DC 端設定。

建立 Organizational Unit 結構

假設結構長這樣

1
2
3
4
yi.local
└── OU: HQ(總部)
├── OU: IT(資訊部)
└── OU: HR(人資部)

GUI

Active Directory Users and Computers

image

yi.local 按右鍵 → New → Organizational Unit

image

Name 叫 HQ 然後 OK

image

建好 HQ 之後,對著它按右鍵 → New → Organizational Unit 像剛剛一樣的方法建立其他項

image

PowerShell 寫法

1
New-ADOrganizationalUnit -Name "HQ" -Path "DC=yi,DC=local" -ProtectedFromAccidentalDeletion $false
1
2
New-ADOrganizationalUnit -Name "IT" -Path "OU=HQ,DC=yi,DC=local" -ProtectedFromAccidentalDeletion $false
New-ADOrganizationalUnit -Name "HR" -Path "OU=HQ,DC=yi,DC=local" -ProtectedFromAccidentalDeletion $false

驗證

image

建立測試使用者帳號

目標:

然後丟到 IT 裡

GUI

以 Emma.Wilson 為例

Active Directory Users and Computers

IT 按右鍵 → New → User

image

image

image

PowerShell 寫法

以 Isabella.Davis 為例

1
2
3
4
5
6
7
8
9
10
New-ADUser -Name "Isabella Davis" `
-GivenName "Isabella" `
-Surname "Davis" `
-SamAccountName "Isabella.Davis" `
-UserPrincipalName "Isabella.Davis@yi.local" `
-Path "OU=IT,OU=HQ,DC=yi,DC=local" `
-AccountPassword (ConvertTo-SecureString "P@ssw0rd2026!" -AsPlainText -Force) `
-Enabled $true `
-PasswordNeverExpires $true `
-ChangePasswordAtLogon $false

驗證

image


加 Domain Admins

1
Add-ADGroupMember -Identity "Domain Admins" -Members "Emma.Wilson"
1
Get-ADGroupMember -Identity "Domain Admins" | Select-Object Name

image

點兩下 Emma Wilson

image

Member Of

image

Add…

Domain Admins

image

Check Names → OK

Apply

image

GPO

image

GUI

Group Policy Management

image

Domain Controllers 右鍵 → Create a GPO in this domain, and Link it here…

image

名字就叫 Deny Local Logon 吧!

image

image

右鍵 → Edit…

會跳出 Group Policy Management Editor

image

note:Computer Configuration vs User Configuration
  • Computer Configuration(電腦設定):套用在「電腦」上的規則,不管誰登入這台電腦都生效
  • User Configuration(使用者設定):套用在「使用者帳號」上的規則,不管在哪台電腦登入都生效

Deny Logon Locally(拒絕本機登入)屬於「電腦設定」底下的使用者權限指派,因為這個規則本質上是「限制某使用者能不能登入到這台/這些電腦」,所以要走 Computer Configuration

Computer Configuration → Policies → Windows Settings → Security Settings → Local Policies → User Rights Assignment

看 Deny log on locally

image

Define these policy settings 打勾 → Add User or Group…

image

Browse

image

Isabella.Davis → Check Names → OK

image

image

OK

image

image

PowerShell 寫法

By Claude,但它說 GUI 比較快所以我這邊 PowerShell 寫法應該不會特別記

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 1. 建立並連結 GPO(跟之前一樣)
New-GPO -Name "Deny Local Logon - Isabella"
New-GPLink -Name "Deny Local Logon - Isabella" -Target "OU=Domain Controllers,DC=yi,DC=local"

# 2. 取得該 GPO 在 SYSVOL 裡的實際路徑
$gpo = Get-GPO -Name "Deny Local Logon - Isabella"
$gpoPath = "\\yi.local\SYSVOL\yi.local\Policies\{$($gpo.Id)}\Machine\Microsoft\Windows NT\SecEdit"

# 3. 建立目錄(如果不存在)
New-Item -Path $gpoPath -ItemType Directory -Force | Out-Null

# 4. 先取得 Isabella.Davis 的 SID
$sid = (Get-ADUser -Identity "Isabella.Davis").SID.Value

# 5. 寫入安全性範本內容(GptTmpl.inf)
$iniContent = @"
[Unicode]
Unicode=yes
[Version]
signature="`$CHICAGO`$"
Revision=1
[Privilege Rights]
SeDenyInteractiveLogonRight = *$sid
"@
Set-Content -Path "$gpoPath\GptTmpl.inf" -Value $iniContent -Encoding Unicode

# 6. 更新 GPO 版本號,讓系統知道內容有變更(觸發套用)
$gpo | Set-GPO -Description "Deny logon locally updated via script"

# 7. 立即刷新群組原則
gpupdate /force

改 AdminSDHolder 本身的 ACL

image

直接改 Emma.Wilson 的 ACL 沒用,因為 SDProp 把 AdminSDHolder 上的權限範本,強制覆蓋貼到這些成員帳號自己的 ACL 上,所以直接改 60 分鐘後就會打回原形

GUI

ADSI Edit

image

右鍵 → Connect to…

image

就直接預設不改任何東西

Default naming context → OK

image

DC=yi,DC=local → CN=System → CN=AdminSDHolder

image

右鍵 → Properties 看 Security 點 Add…

image

Domain Users → Check Names → OK

image

Apply → OK

LAPS

image

擴充 AD Schema

1
Update-LapsADSchema -Confirm:$false

讓這個 OU 底下的電腦帳號擁有寫入自己 ms-LAPS-Password 等屬性的權限

1
Set-LapsADComputerSelfPermission -Identity "OU=Domain Controllers,DC=yi,DC=local"

Group Policy Management

Domain Controllers 右鍵 → Create a GPO in this domain, and Link it here…

image

叫 LAPS Policy 好了

image

右鍵 Edit

Computer Configuration → Policies → Administrative Templates → System → LAPS

image

點兩下 Password settings 然後 Enable 後根據題目要的去改

image

Configure password backup directory

選 Enabled → Active Directory

image

預設是 Azure AD,這樣密碼會存到微軟雲端的 Entra ID,需要額外的雲端訂閱、網路連線到 Azure,但現在純本地端所以用 AD

image

image


相似題目補充

← Back to Home