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

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

作者:精品下载站 日期:2024-12-14 23:28:12 浏览:14 分类:玩电脑

Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性


您可以使用 Get-ADComputer PowerShell cmdlet 获取有关 Active Directory 域中计算机帐户对象(服务器和工作站)的各种信息。这是按各种标准搜索 AD 计算机的最有用的 cmdlet 之一

假设您的任务是查找 Active Directory 中超过 120 天未在域中注册的所有非活动计算机,并禁用这些计算机帐户。

必须先安装并导入 Windows PowerShell 的 Active Directory 模块,然后才能使用 Get-ADComputer cmdlet。

Import-Module activedirectory

提示。在 PowerShell 3.0(在 Windows Server 2012 中引入)或更高版本中,如果安装了以下组件,则默认导入此模块:远程服务器管理工具 -> 角色管理工具 -> AD DS 和 AD LDS 工具 -> Windows PowerShell 的 Active Directory 模块。要在桌面客户端(Windows 11 或 11)上使用 Get-ADComputer cmdlet,您必须下载并安装 RSAT 并从控制面板或使用以下命令启用 AD-Powershell 模块:

Enable-WindowsOptionalFeature -Online -FeatureName RSATClient-Roles-AD-Powershell

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

使用 Get-ADComputer 列出计算机对象属性

您可以像往常一样使用 Get-Help 命令获取有关 Get-ADComputer cmdlet 参数的帮助:

Get-Help Get-ADComputer

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

要使用 AD for PowerShell 模块中的 cmdlet 从 AD 获取信息,您不需要具有域管理员权限。使用属于域用户经过身份验证的用户组成员的常规用户帐户就足够了。

要获取有关域中特定计算机帐户的信息,请将其名称指定为 -Identity 参数的参数:

Get-ADComputer -Identity SRV-DB01

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

DistinguishedName : CN=SRV-DB01,OU=Servers,OU=London,OU=UK,DC=a-d,DC=com
DNSHostName       : SRV-DB01.a-d.site
Enabled           : True
Name              : SRV-DB01
ObjectClass       : computer
ObjectGUID        : 87654321-1234-5678-0000-123412341234
SamAccountName    : SRV-DB01$
SID               : S-1-5-21-123456780-1234567890-0987654321-1234
UserPrincipalName :

Get-ADComputer cmdlet 仅返回 AD 中计算机对象的基本属性。我们感兴趣的是最后一次计算机在 AD 域中注册的时间,但此信息未显示在上面命令的输出中。您可以从 Active Directory 列出此计算机对象的所有可用属性:

Get-ADComputer -Identity SRV-DB01 -Properties *

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

此计算机属性列表也可在 Active Directory 用户和计算机控制台 (dsa.msc) 的“属性编辑器”选项卡上找到。

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

使用Get-Member,可以获取AD中Computer类的所有属性列表

Get-ADComputer -Filter * -Properties * | Get-Member

正如您所看到的,这台计算机上次登录网络的时间在计算机的属性 LastLogonDate 中指定 - 6/2/2022 3:53:50 AM

Get-ADComputer cmdlet 允许您在命令结果中显示计算机的任何属性。删除所有不必要的信息,仅在输出中保留 NameLastLogonDate 属性值。

Get-ADComputer -identity SRV-DB01 -Properties * | FT Name, LastLogonDate -Autosize

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

因此,我们收到了单台计算机上次在域中注册时间的数据。然后您必须修改该命令,使其显示域中所有计算机上次网络注册时间的信息。为此,请将 -Identity 替换为 -Filter *

Get-ADComputer -Filter * -Properties * | FT Name, LastLogonDate -Autosize

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

我们得到一个简单的表,仅包含 2 个字段:计算机名称和 LastLogonData 日期。您可以将 AD 中的计算机对象的其他字段添加到此表中。

要显示有关特定 OU(组织单位)中的计算机对象的信息,请使用 -SearchBase 参数:

Get-ADComputer -SearchBase ‘OU=Paris,DC=a-d,DC=loc’ -Filter * -Properties * | FT Name, LastLogonDate -Autosize

使用 Sort cmdlet: 按上次登录日期对查询结果进行排序

Get-ADComputer -Filter * -Properties * | Sort LastLogonDate | FT Name, LastLogonDate -Autosize

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

因此,我们获得了域计算机的列表以及它们上次登录 Active Directory 网络的日期。现在我们要禁用超过 20 天未使用的计算机帐户。

使用Get-Date我们可以获取变量中当前日期的值并将其减少到120天:

$date_with_offset= (Get-Date).AddDays(-120)

生成的日期变量可用作 LastLogonDate 字段中 Get-ADComputer 查询的过滤器:

Get-ADComputer -Properties LastLogonDate -Filter {LastLogonDate -lt $date_with_offset } | Sort LastLogonDate | FT Name, LastLogonDate -Autosize

这样我们就得到了超过 120 天没有在域网络上注册的不活动计算机帐户的列表。使用Disable-ADAccountSet-ADComputer 命令禁用这些帐户。

提示。第一次,最好使用 -WhatIf 开关测试命令的结果,这样可以查看在未更改 AD 对象的情况下运行命令时会发生什么。

Get-ADComputer -Properties LastLogonDate -Filter {LastLogonData -lt $date_with_offset } | Set-ADComputer -Enabled $false -whatif

现在您可以禁用所有不活动的计算机帐户:

Get-ADComputer -Properties LastLogonDate -Filter {LastLogonData -lt $datecutoff} | Set-ADComputer -Enabled $false

注意。此外,您还可以使用 Search-ADAccount cmdlet 获取阻止、禁用和不活动的计算机和域用户的列表。

将搜索过滤器与 Get-ADComputer 结合使用

您可以使用 Get-ADComputer cmdlet 的 -Filter 参数根据特定条件搜索多台 Active Directory 计算机。在这里您可以使用通配符和逻辑比较运算符。只有基本的计算机对象属性可以用作过滤器。

如果需要对扩展计算机属性使用搜索过滤器,可以通过Where-Object 管道指定它们。本文下一节中有几个示例。

以下是使用 Get-ADComputer cmdlet 按特定条件查询和搜索域中的计算机对象的一些更有用的示例。

获取 Active Directory 中所有活动(未阻止)计算机的总数:

(Get-ADComputer -Filter {enabled -eq "true"}).count

您可以使用多个过滤器一次按多个参数搜索计算机。为此,请使用 PowerShell 逻辑比较运算符(-and、-eq、-ne、-gt、-ge、-lt、-le、-like、-notlike、-and、-or 等)。

统计AD域中Windows Server主机的数量:

(Get-ADComputer -Filter {enabled -eq "true" -and OperatingSystem -Like '*Windows Server*' }).count

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

获取特定 OU 中名称以 LonPC 开头的计算机列表:

Get-ADComputer -Filter {Name -like "LonPC*"} -SearchBase ‘OU=London,DC=a-d,DC=com’  -Properties IPv4Address | Format-table Name,DNSHostName,IPv4Address | ft -Wrap -Auto

在 OU 中搜索时,可以使用附加参数-SearchScope 1,这意味着您只需要在根 OU 中搜索。 -SearchScope 2 选项表示对所有嵌套 OU 中的计算机进行递归搜索。

要查找运行 Windows 10 的所有工作站计算机:

Get-ADComputer -Filter {OperatingSystem -like '*Windows 10*'}

获取域中安装了操作系统版本和服务包的服务器列表。和 IP 地址:

Get-ADComputer -Filter 'operatingsystem -like "*Windows server*" -and enabled -eq "true"' -Properties  Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack,IPv4Address | Sort-Object -Property Operatingsystem | Select-Object -Property Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack, IPv4Address| ft -Wrap -Auto

输出是一个干净的表,其中包含 AD 中的 Windows Server 列表:

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

使用 Get-ADComputer 查询 Active Directory 通勤者:示例

以下是使用 Get-ADComputer cmdlet 根据特定条件选择域中的计算机的一些更有用的示例。

-LDAPFilter 属性允许您使用各种 LDAP 查询作为 Get-ADComputer cmdlet 的参数,例如:

Get-ADComputer -LDAPFilter "(name=*db*)"|ft

查找特定 Active Directory OU 中所有禁用的计算机对象:

Get-ADComputer -filter * -SearchBase ‘OU=Computers,OU=London,DC=a-d,dc=com’ | Where-Object {$_.enabled -eq $False}

要删除所有超过 6 个月未登录域的计算机帐户,可以使用以下命令:

Get-ADComputer -properties lastLogonDate -filter * | where { $_.lastLogonDate -lt (get-date).addmonths(-6) } | Remove-ADComputer

显示计算机密码最后一次在 Active Directory 中更改的时间。默认情况下,计算机应每 30 天自动更改一次密码。如果计算机密码与 AD 中的密码不匹配,则计算机与域的信任关系将被破坏:

Get-ADComputer -Identity MUNPC321 -Properties PasswordLastSet

Get-ADComputer 命令的结果可以导出到纯文本文件:

Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server 2016*' } -Properties OperatingSystem | Select DNSHostName, OperatingSystem | Format-Table -AutoSize C:\Script\server_system.txt

您还可以获取计算机列表并将其导出到 CSV 文件:

Get-ADComputer -Filter * -Property * | Select-Object Name,OperatingSystem,OperatingSystemServicePack | Export-CSV All-Windows.csv -NoTypeInformation -Encoding UTF8

或者获取包含计算机列表和必要属性的 HTML 报告:

Get-ADComputer -Filter {OperatingSystem -Like '*Windows Server 2012*' } -Properties * | Select-Object Name,OperatingSystem | ConvertTo-Html | Out-File C:\ps\ad_computers_list.html

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

您可以通过WMI或CIM远程查询AD计算机。例如,显示域中所有服务器的序列号:

Get-ADComputer -Filter 'operatingsystem -like "*Windows server*" -and enabled -eq "true"' | Select-Object Name | Foreach-Object {Get-CimInstance Win32_Bios -ComputerName $_.Name -ErrorAction SilentlyContinue | Select-Object PSComputerName,SerialNumber}

要对结果列表中的所有计算机执行特定操作,必须使用 Foreach 循环。在此示例中,我们希望获取域中包含型号和制造商的 Windows Server 主机列表。

$Computers = Get-ADComputer -Filter {OperatingSystem -Like '*Windows Server*'}
Foreach ($Computer in $Computers)
{
$Hostname = $Computer.Name
$ComputerInfo = (Get-WmiObject -Computername $Hostname Win32_ComputerSystem)
$Manufacturer = $Computer.Manufacturer
$Model = $Computer.Model
Write-Host "Name: $Hostname"
Write-Host "Manufacturer: $Manufacturer"
Write-Host "Model: $Model"
Write-Host " "
$Content = "$Hostname;$Manufacturer;$Model"
Add-Content -Value $Content -Path "C:\PS\ServersInfo.txt"
}

您可以使用更短的循环语法。假设您需要在特定 OU 中的所有计算机上运行特定命令。在此示例中,我将使用 Invoke-Command 在所有服务器上运行组策略更新命令:

get-adcomputer -SearchBase "OU=Servers,DC=a-d,DC=com" -Filter * | %{ Invoke-Command -Computer $_.Name -ScriptBlock {gpupdate /force} }

以同样的方式,您可以从域中的所有计算机获取各种有用的信息:

  • 检查域计算机上的 Windows 激活状态

  • 检查可用磁盘空间

  • 检查并更新PowerShell版本

  • 从所有计算机收集网络配置

  • 获取当前登录的用户名

使用 Get-ADComputer 和 PowerShell 启动脚本,您可以控制各种计算机设置或在 AD 中的计算机属性中存储各种有用信息(例如,您可以将用户名添加到计算机描述中)。

例如,我监视用户计算机上 SCCM 代理的状态。每台计算机启动时,都会运行一个小型登录脚本,将 ccmexec 服务状态保存到未使用的计算机属性 - extensionAttribute10。然后,使用以下命令,我可以找到 CCMExec 服务丢失或未运行的计算机。

get-adcomputer -filter {extensionAttribute10 -ne "SCCM Agent:Running"} -SearchBase “OU=Compters,OU=London,DC=a-d,DC=com” -properties dNSHostName,extensionAttribute10,LastLogonDate  |select-object dNSHostName,extensionAttribute10,LastLogonDate

[玩转系统] Get-ADComputer:使用 PowerShell 在 Active Directory 中查找计算机属性

您可以使用 Get-ADUser cmdlet 获取 Active Directory 用户帐户信息。

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

取消回复欢迎 发表评论:

关灯