ค้นหาเว็บไซต์

PowerShell Active Directory | การติดตั้งโมดูล Active Directory | ตัวอย่าง


ข้อมูลเบื้องต้นเกี่ยวกับ PowerShell Active Directory

การจัดการ Active Directory เป็นส่วนสำคัญของผู้ดูแลระบบ windows ผู้ดูแลระบบไม่สามารถเพิ่มหรือลบผู้ใช้ออกจากโฆษณาด้วยตนเองได้ การดำเนินการทั่วไปประเภทนี้ต้องเป็นไปโดยอัตโนมัติ และนั่นคือสิ่งที่ PowerShell นำเสนออย่างแน่นอน มีโมดูลแยกต่างหากซึ่งมี cmdlets ที่ให้ผู้ดูแลระบบดำเนินกิจกรรมเหล่านี้ได้ บทความนี้จะแสดงให้เห็นว่า PowerShell ใช้งานอย่างไรกับ Active Directory และ cmdlets ยอดนิยมบางส่วนที่มีอยู่ในโมดูล Active Directory ของ PowerShell

การติดตั้งโมดูล Active Directory

สิ่งสำคัญคือต้องนำเข้าหรือติดตั้งโมดูล Active Directory ในเครื่องเพื่อเข้าถึง cmdlets ซึ่งสามารถทำได้โดยการดาวน์โหลดแพ็คเกจ RSAT ที่เกี่ยวข้องตามระบบปฏิบัติการของระบบ เริ่มต้นจาก Windows 10 RSAT เป็นส่วนหนึ่งของสิทธิ์ความต้องการและสามารถเปิดใช้งานได้จากคุณสมบัติเสริม

เมื่อเสร็จแล้วจะสามารถนำเข้าโมดูลได้ดังนี้:

นำเข้าโมดูล ActiveDirectory

หากต้องการดูรายการ cmdlets ที่มีอยู่ในโมดูล

รับคำสั่ง -โมดูล ActiveDirectory

cmdlet ที่ใช้บ่อยที่สุดบางส่วนมีดังนี้:

Cmdlet Use Example
New-ADUser This is to create a new user in the AD. New-ADUser -Name “Vignesh” -OtherAttributes @{‘title’=”engineer”;’mail’=”[email ”}.
New-ADGroup This cmdlet is used to create an Active Directory group. New-ADGroup -Name “tested group” -SamAccountName testadmin -GroupCategory Security -GroupScope Global -DisplayName “o365 admin” -Path “CN=Users,DC=admin,DC=Com” -Description “this is a test group”.
New-ADOrganizationalUnit This cmdlet is used to create a new organizational unit. New-ADOrganizationalUnit -Name “testaccounts” -Path “DC=test,DC=COM”.
Add-ADGroupMember To add users to an Active Directory group. Add-ADGroupMember -Identity testadgorup -Members Vignesh,arun,vyapini.
Remove-ADGroupMember To remove users from an active directory group. Remove-ADGroupMember -Identity testadgroup -Members Vignesh,nandhini,vyapini.
Add-Computer This cmdlet is used to join a computer to a specific domain. Add-Computer -DomainName testdomain -Restart.
Enable-ADAccount This cmdlet is used to enable a user or service account in the active directory. Enable-ADAccount -Identity “vignesh”.
Disable-ADAccount This cmdlet is used to disable a user or service in the active directory. Disable-ADAccount -Identity “vignesh”.
Unlock-ADAccount This cmdlet is used to unlock locked out accounts. Unlock-ADAccount -Identity “vignesh”.
Search-ADAccount This cmdlet is used to search for accounts based on condition. Search-ADAccount -AccountDisabled -UsersOnly.

The above cmdlet will retrieve only the user accounts disabled.

Test-ComputerSecureChannel This cmdlet is used to test and repair the connecting channel between the domain and the client system. Test-ComputerSecureChannel-Server “test.com”.

ตัวอย่างของ PowerShell Active Directory

รับด้านล่างเป็นตัวอย่างของ PowerShell Active Directory:

ตัวอย่าง #1

เพิ่มผู้ใช้ใน Active Directory

รหัส:

Write-Host "Welcome to the example of creating users in Active Directory"
$csv=Import-Csv -Path "C:\vignesh\test.csv"
foreach($row in $cvs)
{
$prop = @{
'GivenName' = $row.Gname
'Surname' = $row.Sname
'Name' = $row.Name
'AccountPassword' = (ConvertTo-SecureString 'p@$$123345' -AsPlainText -Force)
'ChangePasswordAtLogon' = $true
}
New-AdUser @prop
Write-Host "User is added to Ad" -ForegroundColor Green
}

สคริปต์ด้านบนจะอ่านคุณสมบัติผู้ใช้จากไฟล์ csv และสร้างผู้ใช้ใน AD

เอาต์พุต:

ตัวอย่าง #2

การเพิ่มและลบผู้ใช้ออกจากกลุ่มโฆษณา

รหัส:

Write-Host "Welcome to the example of adding  users to Active Directory"
$csv=Import-Csv -Path "C:\vignesh\test.csv"
foreach($row in $cvs)
{
if($row.ISadd -eq "true")
{
Add-AdGroupMember -Identity $row.GroupName -Members $row.Member
Write-Host "User is added to the AD Group" -ForegroundColor Green
}
else
{
Remove-AdGroupMember -Identity $row.GroupName -Members $row.Member
Write-Host "User is removed  from the Active Directory Group" -ForegroundColor Red
}
}

เอาต์พุต:

ตัวอย่าง #3

รหัส:

Write-Host "Welcome to the example of Enabling an Active Directory Account"
Enable-ADAccount -Identity "vignesh"
Write-Host "Success: The account vignesh is enabled" -ForegroundColor Green
Enable-ADAccount -Identity "Nandhini"
Write-Host "Success: The account Nandhini is enabled" -ForegroundColor Green
Enable-ADAccount -Identity "Vyapini"
Write-Host "Success: The account vyapini is enabled" -ForegroundColor Green
Enable-ADAccount -Identity "vijay"
Write-Host "Success: The account vijay is enabled" -ForegroundColor Green
Write-Host "Welcome to the example of disbaling an Active Directory Account"
Disable-ADAccount -Identity "vignesh"
Write-Host "Success: The account vignesh is disabled" -ForegroundColor Red
Disable-ADAccount -Identity "Nandhini"
Write-Host "Success: The account Nandhini is disabled" -ForegroundColor Red
Disable-ADAccount -Identity "Vyapini"
Write-Host "Success: The account vyapini is disabled" -ForegroundColor Red
Disable-ADAccount -Identity "vijay"
Write-Host "Success: The account vijay is disabled" -ForegroundColor Red
Write-Host "Welcome to the example of unlocking an Active Directory Account"
Unlock-ADAccount -Identity "sethu"
Write-Host "Success: The account sethu is disabled" -ForegroundColor Yellow
Unlock-ADAccount -Identity "krish"
Write-Host "Success: The account krish is disabled" -ForegroundColor Yellow
Unlock-ADAccount -Identity "siva"
Write-Host "Success: The account siva is disabled" -ForegroundColor Yellow
Unlock-ADAccount -Identity "madhavan"
Write-Host "Success: The account madhavan is disabled" -ForegroundColor Yellow

เอาต์พุต:

ตัวอย่าง #4

รหัส:

Write-Host "Welcome to the example of Removing an Active Directory Account"
Remove-ADUser -Identity "vignesh"
Write-Host "Success: The account vignesh is removed" -ForegroundColor Red
Remove-ADUser -Identity "Nandhini"
Write-Host "Success: The account Nandhini is Removed" -ForegroundColor Red
Remove-ADUser -Identity "Vyapini"
Write-Host "Success: The account vyapini is Removed" -ForegroundColor Red
Remove-ADUser -Identity "vijay"
Write-Host "Success: The account vijay is removed" -ForegroundColor Red
Write-Host "Example of resetting users password"
$newpass=Read-Host "Please Enter the passowrd to be used" -AsSecureString
Set-ADAccountPassword -Identity "Vignesh" -NewPassword $newpass -Reset
Write-Host "Password is reset for vignesh successfully" -ForegroundColor Green
Set-ADAccountPassword -Identity "Nandhini" -NewPassword $newpass -Reset
Write-Host "Password is reset for Nandhini successfully" -ForegroundColor Green
Set-ADAccountPassword -Identity "Vyapini" -NewPassword $newpass -Reset
Write-Host "Password is reset for Vyapini successfully" -ForegroundColor Green
Write-Host "Example of creating a new AD Group"
New-ADGroup -Name "test Admins" -SamAccountName testadmins -GroupCategory Security -GroupScope Global -DisplayName "testadmins" -Path "CN=Users,DC=test,DC=Com" -Description "test"
Write-Host "new test ad group is created" -ForegroundColor Magenta
New-ADGroup -Name "test Admins1" -SamAccountName testadmins1 -GroupCategory Security -GroupScope Global -DisplayName "testadmins1" -Path "CN=Users,DC=test,DC=Com" -Description "test1"
Write-Host "new test ad group1 is created" -ForegroundColor Magenta
New-ADGroup -Name "test Admins2" -SamAccountName testadmins2 -GroupCategory Security -GroupScope Global -DisplayName "testadmins2" -Path "CN=Users,DC=test,DC=Com" -Description "test2"
Write-Host "new test ad group2 is created" -ForegroundColor Magenta
Write-Host "Demo of creating new OU Group"
New-ADOrganizationalUnit -Name "testou1" -Path "DC=test,DC=com"
Write-Host "testou1 is created" -ForegroundColor Yellow
New-ADOrganizationalUnit -Name "testou2" -Path "DC=test,DC=com"
Write-Host "testou2 is created" -ForegroundColor Yellow
New-ADOrganizationalUnit -Name "testou3" -Path "DC=test,DC=com"
Write-Host "testou3 is created" -ForegroundColor Yellow

เอาต์พุต:

บทสรุป

ดังนั้นบทความนี้จึงกล่าวถึงรายละเอียดเกี่ยวกับวิธีใช้ PowerShell เพื่อเชื่อมต่อกับ AD นอกจากนี้เรายังเห็นรายละเอียดเกี่ยวกับ cmdlet ของโมดูล AD ที่ใช้กันทั่วไปบางส่วนพร้อมกับตัวอย่างอีกด้วย มีการแสดงสคริปต์ตัวอย่างเพื่อทำงานอัตโนมัติที่พบบ่อยที่สุด

บทความที่เกี่ยวข้อง