PowerShell

PowerShell betiği ile Active Directory bilgilerini alma

Tek bir çıktıda Active Directory bilgisi nasıl alınır? Örneğin, Active Directory’yi yeni bir sunucuya taşımak istiyorsunuz ve AD bilgilerini almak istiyorsunuz. Veya AD’de kaç iş istasyonu, sunucu veya grup bulunduğunu bilmek ister misiniz? Bu yazıda, PowerShell betiği ile Active Directory bilgilerini nasıl alacağınızı öğreneceksiniz.

İçindekiler

  • AD bilgilerini alma PowerShell betiği
  • AD bilgilerini alma PowerShell komut dosyasını indirin
  • AD bilgilerini al PowerShell betiğini çalıştırın
  • Son

AD bilgilerini alma PowerShell betiği

Get-ADInfo.ps1 PowerShell betiği aşağıdaki AD bilgilerini alır:

  1. Bilgisayarlar (İş İstasyonları + Sunucular)
  2. Iş istasyon -ları
  3. Sunucu
  4. Kullanıcı
  5. Grup
  6. Active Directory orman adı
  7. Active Directory orman modu
  8. Active Directory etki alanı modu
  9. Active Directory şema sürümü
  10. FSMO rol sahipleri

AD bilgilerini alma PowerShell komut dosyasını indirin

Get-ADInfo.ps1 PowerShell betiğini indirin ve C:\scripts klasörüne yerleştirin. Komut dosyaları klasörünüz yoksa bir tane oluşturun.

# Get counts of different types of objects in Active Directory
$Computers = (Get-ADComputer -Filter * | Measure-Object).Count
$Workstations = (Get-ADComputer -Filter { OperatingSystem -notlike "*Server*" } | Measure-Object).Count
$Servers = (Get-ADComputer -Filter { OperatingSystem -like "*Server*" } | Measure-Object).Count
$Users = (Get-ADUser -Filter * | Measure-Object).Count
$Groups = (Get-ADGroup -Filter * | Measure-Object).Count

# Get Active Directory Forest information
$ADForest = (Get-ADDomain).Forest
$ADForestMode = (Get-ADForest).ForestMode
$ADDomainMode = (Get-ADDomain).DomainMode

# Obtain Active Directory Schema version and translate it to the corresponding Windows Server version
$ADVer = Get-ADObject (Get-ADRootDSE).schemaNamingContext -Property objectVersion | Select-Object objectVersion
$ADNum = $ADVer -replace "@{objectVersion=", "" -replace "}", ""

switch ($ADNum) {
    '91' { $srv = 'Windows Server 2025' }
    '88' { $srv = 'Windows Server 2019/Windows Server 2022' }
    '87' { $srv = 'Windows Server 2016' }
    '69' { $srv = 'Windows Server 2012 R2' }
    '56' { $srv = 'Windows Server 2012' }
    '47' { $srv = 'Windows Server 2008 R2' }
    '44' { $srv = 'Windows Server 2008' }
    '31' { $srv = 'Windows Server 2003 R2' }
    '30' { $srv = 'Windows Server 2003' }
}

# Display collected information
Write-host "Active Directory Info" -ForegroundColor Yellow
Write-host ""
Write-Host "Computers  = $Computers" -ForegroundColor Cyan
Write-Host "Workstions = $Workstations" -ForegroundColor Cyan
Write-Host "Servers    = $Servers" -ForegroundColor Cyan
Write-Host "Users      = $Users" -ForegroundColor Cyan
Write-Host "Groups     = $Groups" -ForegroundColor Cyan
Write-host ""
Write-Host "Active Directory Forest Name = "$ADForest -ForegroundColor Cyan
Write-Host "Active Directory Forest Mode = "$ADForestMode -ForegroundColor Cyan
Write-Host "Active Directory Domain Mode = "$ADDomainMode -ForegroundColor Cyan
Write-Host "Active Directory Schema Version is $ADNum which corresponds to $srv" -ForegroundColor Cyan
Write-Host ""
Write-Host "FSMO Role Owners" -ForegroundColor Cyan

# Retrieve FSMO roles individually
$Forest = Get-ADForest
$SchemaMaster = $Forest.SchemaMaster
$DomainNamingMaster = $Forest.DomainNamingMaster
$Domain = Get-ADDomain
$RIDMaster = $Domain.RIDMaster
$PDCEmulator = $Domain.PDCEmulator
$InfrastructureMaster = $Domain.InfrastructureMaster

# Display FSMO role owners
Write-Host "Schema Master         =  $SchemaMaster" -ForegroundColor Cyan
Write-Host "Domain Naming Master  =  $DomainNamingMaster" -ForegroundColor Cyan
Write-Host "RID Master            =  $RIDMaster" -ForegroundColor Cyan
Write-Host "PDC Emulator          =  $PDCEmulator" -ForegroundColor Cyan
Write-Host "Infrastructure Master =  $InfrastructureMaster" -ForegroundColor Cyan

Komut dosyasını çalıştırırken herhangi bir hatayı önlemek için dosyanın engellemesinin kaldırıldığından emin olun. PowerShell betiği çalıştırılırken dijital olarak imzalanmamış hata makalesinde daha fazla bilgi edinin.

Diğer bir seçenek de aşağıdaki kodu kopyalayıp Not Defteri’ne yapıştırmaktır. Get-ADInfo.ps1 adını verin ve C:\scripts klasörüne yerleştirin.

# Get counts of different types of objects in Active Directory
$Computers = (Get-ADComputer -Filter * | Measure-Object).Count
$Workstations = (Get-ADComputer -Filter { OperatingSystem -notlike "*Server*" } | Measure-Object).Count
$Servers = (Get-ADComputer -Filter { OperatingSystem -like "*Server*" } | Measure-Object).Count
$Users = (Get-ADUser -Filter * | Measure-Object).Count
$Groups = (Get-ADGroup -Filter * | Measure-Object).Count

# Get Active Directory Forest information
$ADForest = (Get-ADDomain).Forest
$ADForestMode = (Get-ADForest).ForestMode
$ADDomainMode = (Get-ADDomain).DomainMode

# Obtain Active Directory Schema version and translate it to the corresponding Windows Server version
$ADVer = Get-ADObject (Get-ADRootDSE).schemaNamingContext -Property objectVersion | Select-Object objectVersion
$ADNum = $ADVer -replace "@{objectVersion=", "" -replace "}", ""

switch ($ADNum) {
    '91' { $srv = 'Windows Server 2025' }
    '88' { $srv = 'Windows Server 2019/Windows Server 2022' }
    '87' { $srv = 'Windows Server 2016' }
    '69' { $srv = 'Windows Server 2012 R2' }
    '56' { $srv = 'Windows Server 2012' }
    '47' { $srv = 'Windows Server 2008 R2' }
    '44' { $srv = 'Windows Server 2008' }
    '31' { $srv = 'Windows Server 2003 R2' }
    '30' { $srv = 'Windows Server 2003' }
}

# Display collected information
Write-host "Active Directory Info" -ForegroundColor Yellow
Write-host ""
Write-Host "Computers  = $Computers" -ForegroundColor Cyan
Write-Host "Workstions = $Workstations" -ForegroundColor Cyan
Write-Host "Servers    = $Servers" -ForegroundColor Cyan
Write-Host "Users      = $Users" -ForegroundColor Cyan
Write-Host "Groups     = $Groups" -ForegroundColor Cyan
Write-host ""
Write-Host "Active Directory Forest Name = "$ADForest -ForegroundColor Cyan
Write-Host "Active Directory Forest Mode = "$ADForestMode -ForegroundColor Cyan
Write-Host "Active Directory Domain Mode = "$ADDomainMode -ForegroundColor Cyan
Write-Host "Active Directory Schema Version is $ADNum which corresponds to $srv" -ForegroundColor Cyan
Write-Host ""
Write-Host "FSMO Role Owners" -ForegroundColor Cyan

# Retrieve FSMO roles individually
$Forest = Get-ADForest
$SchemaMaster = $Forest.SchemaMaster
$DomainNamingMaster = $Forest.DomainNamingMaster
$Domain = Get-ADDomain
$RIDMaster = $Domain.RIDMaster
$PDCEmulator = $Domain.PDCEmulator
$InfrastructureMaster = $Domain.InfrastructureMaster

# Display FSMO role owners
Write-Host "Schema Master         =  $SchemaMaster" -ForegroundColor Cyan
Write-Host "Domain Naming Master  =  $DomainNamingMaster" -ForegroundColor Cyan
Write-Host "RID Master            =  $RIDMaster" -ForegroundColor Cyan
Write-Host "PDC Emulator          =  $PDCEmulator" -ForegroundColor Cyan
Write-Host "Infrastructure Master =  $InfrastructureMaster" -ForegroundColor Cyan

AD bilgilerini al PowerShell betiğini çalıştırın

PowerShell’i yönetici olarak çalıştırın. Ardından, Active Directory bilgilerini toplamak için PowerShell betiğini çalıştırın.

C:\scripts\.\Get-ADInfo.ps1

Organizasyonumuzda çıktı bu şekilde görünüyor.

Active Directory Info

Computers = 5
Workstions = 1
Servers = 4
Users = 74
Groups = 88

Active Directory Forest Name = onder.local
Active Directory Forest Mode = Windows2016Forest
Active Directory Domain Mode = Windows2016Domain
Active Directory Schema Version is 88 which corresponds to Windows Server 2019/Windows Server 2022

FSMO Role Owners
Schema Master = DC01-2019.onder.local
Domain Naming Master = DC01-2019.onder.local
RID Master = DC01-2019.onder.local
PDC Emulator = DC01-2019.onder.local
Infrastructure Master = DC01-2019.onder.local

PowerShell betiği ile Active Directory bilgilerini nasıl alacağınızı öğrendiniz. Active Directory’de çok fazla bilgi vardır ve bilgileri PowerShell veya GUI’de tek tek aramak çok zaman alır. Bir PS komut dosyası çalıştırmak ve hepsini tek bir çıktıda bulundurmak zaman kazandırır ve bakması daha kolaydır.

Yorum Yazın