网站搜索

使用 PowerShell 进行 Active Directory 查询


style="letter-spacing: 0px; text-transform: unset; background-color:透明;">在 Windows 中查询信息时,PowerShell 是一个强大的环境。 PowerShell 可以提供重要帮助,不仅可以帮助脚本编辑服务器设置或工作负载,还可以帮助用于全面的信息检索和报告生成。在本文中,我们将展示一些可以从 Active Directory (AD) 收集大量信息的 PowerShell cmdlet。此类查询结果对于故障排除也很有趣。顺便说一句,ScriptRunner 具有内置查询,可以完成与脚本化 PowerShell 查询相同但更快的查询 - 并由您自己的脚本化查询进行补充(使用“带有脚本”功能)。

如果 Windows 10/11 或 Windows Server 2019/2022 中安装了 Active Directory 管理模块,您还可以在 PowerShell 中访问特定的 Active Directory (AD) 信息。 Get-Command Get-Ad* 命令已经显示了许多可以显示 Active Directory 信息的 cmdlet。

这也可以通过网络顺利运行。 Get-Command -Module ActiveDirectory cmdlet 显示可以管理 Active Directory 的所有 cmdlet。命令Get-Module -ListAvailable再次显示所有模块。这样,其他模块的名称也可以显示,并且可以使用 Get-Command 读取它们的 cmdlet。


显示用户帐户的数据

可以使用过滤器扩展各个 Get cmdlet。例如,如果您想显示当前未禁用的所有用户,您可以使用:

get-aduser -ldapfilter "(&(&(objectCategory=user)(userAccountControl=512)))" | where-object -property enabled -eq truepowershell-ad-infos-01

从 AD 获取用户信息

显示 Active Directory 用户数量(AD用户),您可以使用:

 (Get-ADUser -Filter "Name -like '*'").count

如果您想显示用户帐户的上次登录时间,PowerShell 也可以使用不同的参数:

Get-ADUser "<Name>" -Properties LastLogonDate | FT -Property Name, LastLogonDate -A

所有用户帐户的登录日期也可以在 PowerShell 中显示为列表:

Get-ADUser -Filter {(Enabled -eq $true)} -Properties LastLogonDate | select samaccountname, Name, LastLogonDate | Sort-Object LastLogonDate

可以使用 Search-ADAccount -lockedout 显示锁定的用户帐户。通过 PowerShell,帐户也可以再次解锁,例如使用 Get-ADUser -Identity joost |解锁-ADAccount

显示 Active Directory 中的计算机帐户

要同时显示 AD 中的计算机帐户,可以使用其他 cmdlet。通过管道,可以将结果传递给“Select”,并可以过滤数据以获取所需的信息:

 Get-ADComputer -filter 'name -like "*"' -Properties * | select name,OperatingSystem,IPv4Address

与用户帐户一样,您可以对 Active Directory 中的计算机帐户进行计数:

(Get-ADComputer -Filter "Name -like '*'").count

查询域控制器

当从 Active Directory 读取信息时,环境中的域控制器发挥着重要作用。要查看有关域控制器的数据,请使用 Get-ADDomaincontroller cmdlet。如果要显示环境中所有域控制器的信息,还可以过滤数据,例如:

(Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ }

在 PowerShell 中显示域控制器信息

稍后我们将讨论此 cmdlet 的更多可能性。

您可以使用免费的 Microsoft Active Directory 文档脚本来大致了解 PowerShell 脚本可以执行哪些操作来提取信息。您只需在解压后运行该脚本即可。该脚本还可以生成报告。

使用 PowerShell 脚本,您还可以从 AD 读取大量信息

本部分中的另一个脚本是“ADinfo.ps1”。

启动后,该脚本还会显示来自 Active Directory 的信息。

从PowerShell输出信息到HTML文件对于PowerShell来说也是没有问题的

然而,PowerShell 提供了更多选项。还可以读出组策略,包括创建报告。其命令如下所示:

Get-GPOReport -All -Domain "joos.int" -ReportType HTML -Path "C:\temp\report.html"

在 PowerShell 中,您还可以使用 Get-ADDefaultDomainPasswordPolicy 查看域中的密码策略设置。这些设置在相应策略的组策略管理控制台 (gpmc.msc) 中进行控制。密码策略配置路径为“计算机配置/策略/Windows设置/安全设置/帐户策略/密码策略”

查询DNS数据以及域和整体结构的信息

Active Directory 和 DNS 紧密配合。如果 Active Directory 中存在问题,或者需要 DNS 区域的文档,则可以使用 Get-DnsServerZone -ComputerName 轻松完成。

Active Directory 中的 DNS 记录在允许域控制器相互连接方面发挥着重要作用。也可以在命令行中使用“nslookup”进行检查:

  1. 启动“nslookup”。
  2. 输入“设置类型=全部”。
  3. 键入“_ldap._tcp.dc._msdcs.<域名称>”,例如“_ldap._tcp.dc._msdcs.joos.int”。

在 PowerShell 中,您还可以使用各种 cmdlet 从各个域检索数据。 Get-ADDomain 和 Get-ADForest cmdlet 显示有关林和域的信息。管道可用于将结果从 cmdlet 传递到其他 cmdlet,例如 Get-ADDomainController。例如,如果您想要显示每个域的 FSMO 角色,这非常有用。在每个域中,您可以使用以下命令显示三个 FSMO 角色:

Get-ADDomain | Select InfrastructureMaster, RID-Master, PDCEmulator

每个林中架构主机和域名主机仅存在一次。此信息又可以使用 CMDlet Get-ADForest 显示:

Get-ADForest | Select-Object DomainNamingMaster, SchemaMaster

Get-ADDomain 最初显示域的操作主机(基础设施、RID 和 PDC)、DNS 名称、域控制器、操作模式、主要组织单位和其他信息。

<Get-ADForest cmdlet 还显示域名主、架构主、位置、UPN 后缀和域。这两个 cmdlet 在几秒钟内提供有关域、结构和林的所有相关信息。如果要显示单个域控制器的 FSMO 角色,可以使用:

(Get-ADDomainController).OperationMasterRoles

Get-ADDomainController -Filter {OperationMasterRoles -like "PDC*"} | Select Name

如果您想获得要更深入地了解域控制器,请使用域控制器的 Get-ADDomaincontroller cmdlet。在这里,您还可以查看域控制器上活动的 IP 地址和 FSMO 角色。此外,您还可以查看哪些域控制器配置为全局编录以及服务器上安装的操作系统的版本。域控制器分配的位置以及服务器在网络中侦听的端口也可以在此处找到。

查询Active Directory中域控制器的信息

在 PowerShell 中查询和设置删除保护< /跨度>

Active Directory 中的删除保护可防止对象被意外删除。可以在 PowerShell 中检查保护是否处于活动状态。还可以通过以下方式激活对象的保护:

Get-ADObject ‹DN of object› -Properties ProtectedFromAccidentalDeletion

可以通过以下方式激活删除保护:

Set-ADObject ‹DN of object› -ProtectedFromAccidentalDeletion $true

Good2know

 

To go along with this topic, check out this ScriptRunner webinar:

Active Directory management - Easy as that with PowerShell

Managing Active Directory users and groups is one of the central, recurring tasks of IT administration. In this webinar, we will show you how to standardize, automate and delegate these tasks in a time-saving way using PowerShell.

How to query Active Directory with ScriptRunner

 

Query Screenshot ScriptRunner
ScriptRunner already comes with different, ready to use types of queries. Common use cases are AD queries, which can be executed directly within the Windows domain.

The appropriate queries are also available for Azure straight away. It is also possible to execute self-written scripts as queries by selecting the option "...with a script".

With "List of Entries", predefined lists can be stored as selection, the last option "File of Entries" can be used to retrieve options in a file e.g. *.CSV.

相关文章