Tìm kiếm trang web

Thư mục hoạt động PowerShell | Cài đặt mô-đun Active Directory | Ví dụ


Giới thiệu về PowerShell Active Directory

Quản lý Active Directory là một phần không thể thiếu của quản trị viên windows. Quản trị viên không thể thêm hoặc xóa người dùng khỏi AD theo cách thủ công. Loại hành động chung này cần được tự động hóa và đó chính xác là những gì PowerShell cung cấp. Nó có một mô-đun riêng có các lệnh ghép ngắn cho phép quản trị viên thực hiện các hoạt động này. Bài viết này sẽ chỉ ra cách PowerShell được sử dụng để hoạt động với Active Directory và một số lệnh ghép ngắn phổ biến có sẵn trong mô-đun Active Directory của PowerShell.

Cài đặt mô-đun Active Directory

Điều quan trọng là phải nhập hoặc cài đặt mô-đun thư mục hoạt động trong máy để có quyền truy cập vào các lệnh ghép ngắn. Điều này có thể được thực hiện bằng cách tải xuống gói RSAT tương ứng dựa trên hệ điều hành của hệ thống. Bắt đầu từ Windows 10, RSAT là một phần của quyền yêu cầu và có thể được kích hoạt từ các tính năng tùy chọn.

Khi đã xong, mô-đun có thể được nhập như sau:

Nhập mô-đun ActiveDirectory

Để xem danh sách các lệnh ghép ngắn có sẵn trong mô-đun.

get-command -module ActiveDirectory

Một số lệnh ghép ngắn được sử dụng phổ biến nhất như sau:

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”.

Ví dụ về PowerShell Active Directory

Dưới đây là các ví dụ về PowerShell Active Directory:

Ví dụ số 1

Thêm người dùng vào Active Directory.

Mã:

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
}

Tập lệnh trên sẽ đọc thuộc tính người dùng từ tệp csv và tạo người dùng trong AD.

Đầu ra:

Ví dụ #2

Thêm và xóa người dùng khỏi Nhóm AD.

Mã:

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
}
}

Đầu ra:

Ví dụ #3

Mã:

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

Đầu ra:

Ví dụ #4

Mã:

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

Đầu ra:

Phần kết luận

Vì vậy, bài viết đã trình bày chi tiết về cách sử dụng PowerShell để kết nối với AD. Chúng tôi cũng đã xem chi tiết về một số lệnh ghép ngắn mô-đun AD thường được sử dụng cùng với các ví dụ. Các tập lệnh mẫu đã được hiển thị để thực hiện các tác vụ tự động phổ biến nhất.

Bài viết liên quan