当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 脚本获取 Active Directory 信息

作者:精品下载站 日期:2024-12-14 18:41:46 浏览:15 分类:玩电脑

使用 PowerShell 脚本获取 Active Directory 信息


如何在一个输出中获取 Active Directory 信息?例如,您想要将 Active Directory 迁移到新服务器,并且想要获取 AD 信息。或者您想知道 AD 中有多少个工作站、服务器或组?在本文中,您将了解如何使用 PowerShell 脚本获取 Active Directory 信息。

获取 AD 信息 PowerShell 脚本

Get-ADInfo.ps1 PowerShell 脚本将获取以下 AD 信息:

  1. 计算机(工作站+服务器)

  2. 工作站

  3. 服务器

  4. 用户

  5. 团体

  6. Active Directory 林名称

  7. Active Directory 林模式

  8. Active Directory 域模式

  9. Active Directory 架构版本

  10. FSMO 角色所有者

下载获取 AD 信息 PowerShell 脚本

下载 Get-ADInfo.ps1 PowerShell 脚本并将其放置在 C:\scripts 文件夹中。如果您没有脚本文件夹,请创建一个。

确保该文件未被阻止,以防止运行脚本时出现任何错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Get-ADInfo.ps1 并将其放置在 C:\scripts 文件夹中。

<#
    Author:       ALI TAJRAN
    Website:      www.a-d.site
    LinkedIn:     linkedin.com/in/a-d
    Date:         01/07/2023
    Description:  Get Active Directory information
#>

# Get AD info
$Computers = (Get-ADComputer -Filter *).count
$Workstations = (Get-ADComputer -LDAPFilter "(&(objectClass=Computer)(!operatingSystem=*server*))" -Searchbase (Get-ADDomain).distinguishedName).count
$Servers = (Get-ADComputer -LDAPFilter "(&(objectClass=Computer)(operatingSystem=*server*))" -Searchbase (Get-ADDomain).distinguishedName).count
$Users = (Get-ADUser -Filter *).count 
$Groups = (Get-ADGroup -Filter *).Count
$ADForest = (Get-ADDomain).Forest
$FSMO = netdom query FSMO
$ADForestMode = (Get-ADForest).ForestMode
$ADDomainMode = (Get-ADDomain).DomainMode
$ADVer = Get-ADObject (Get-ADRootDSE).schemaNamingContext -property objectVersion | Select objectVersion
$ADNUM = $ADVer -replace "@{objectVersion=", "" -replace "}", ""

If ($ADNum -eq '88') { $srv = 'Windows Server 2019/Windows Server 2022' }
ElseIf ($ADNum -eq '87') { $srv = 'Windows Server 2016' }
ElseIf ($ADNum -eq '69') { $srv = 'Windows Server 2012 R2' }
ElseIf ($ADNum -eq '56') { $srv = 'Windows Server 2012' }
ElseIf ($ADNum -eq '47') { $srv = 'Windows Server 2008 R2' }
ElseIf ($ADNum -eq '44') { $srv = 'Windows Server 2008' }
ElseIf ($ADNum -eq '31') { $srv = 'Windows Server 2003 R2' }
ElseIf ($ADNum -eq '30') { $srv = 'Windows Server 2003' }

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
$FSMO

运行获取 AD 信息 PowerShell 脚本

以管理员身份运行 PowerShell。更改脚本文件夹的路径。然后,运行 PowerShell 脚本来收集 Active Directory 信息。

PS C:\> cd c:\scripts
PS C:\scripts> .\Get-ADInfo.ps1

这就是我们组织中输出的样子。

Active Directory Info

Computers =     8
Workstions =    3
Servers =       5
Users =         5143
Groups =        88

Active Directory Forest Name =   exoip.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.exoip.local
Domain naming master        DC01-2019.exoip.local
PDC                         DC01-2019.exoip.local
RID pool manager            DC01-2019.exoip.local
Infrastructure master       DC01-2019.exoip.local
The command completed successfully.

这是它的外观的屏幕截图。

[玩转系统] 使用 PowerShell 脚本获取 Active Directory 信息

就是这样!

了解更多:使用 PowerShell 获取所有域控制器 »

结论

您学习了如何使用 PowerShell 脚本获取 Active Directory 信息。 Active Directory 中有大量信息,在 PowerShell 或 GUI 中一一搜索信息需要花费大量时间。运行 PS 脚本并将其全部输出到一个输出中可以节省时间并且更易于查看。

您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 将 AD 用户导出为 CSV。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯