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

[玩转系统] Get-ADComputer- 通过示例查找 OU 中的计算机详细信息

作者:精品下载站 日期:2024-12-14 17:01:27 浏览:13 分类:玩电脑

Get-ADComputer- 通过示例查找 OU 中的计算机详细信息


PowerShell 中的 Get-AdComputer cmdlet 用于查找 Active Directory 中的一台或多台计算机或查找 OU(组织单位)中的计算机。使用PowerShell Get-AdComputer获取OU中的计算机列表,通过操作系统获取adcomputer过滤器。

Get-AdComputer 执行搜索以检索活动目录中的多台计算机或返回一台计算机。

Get-AdComputer cmdlet 检索默认的计算机对象属性集。

您可以使用 Get-AdComputer Properties 参数来检索其他属性。

PowerShell Get-AdComputer Cmdlet 具有一个 Identity 参数,用于从 Active Directory 检索特定计算机对象。

如果要根据搜索条件检索一个或多个计算机对象,请使用 FilterLDAPFilter 参数。

使用 get-adcomputer 过滤器和 get-adcomputer ldapfilter,您可以获取 OU 中的所有计算机。

先决条件

要使用 Get-AdComputer cmdlet,您的系统需要满足以下要求:

  • 要安装的 PowerShell Active Directory 模块
  • 具有管理员访问权限或有足够权限读取 Active Directory 信息的用户

提示:要了解系统中可用的 PowerShell 模块,请在 PowerShell ISE 中运行以下命令

Get-Module -ListAvailable

此命令返回系统中已安装且可用的所有模块。如果 Active Directory 模块不可用,请按照 Active Directory 安装步骤操作。

酷提示:如何使用 PowerShell 获取证书!

Get-AdComputer 语法

PowerShell 中的 Get-Adcomputer cmdlet 从 Active Directory 返回一个或多个计算机对象。

语法

Get-ADComputer
   [-AuthType <ADAuthType>]
   [-Credential <PSCredential>]
   -Filter <String>
   [-Properties <String[]>]
   [-ResultPageSize <Int32>]
   [-ResultSetSize <Int32>]
   [-SearchBase <String>]
   [-SearchScope <ADSearchScope>]
   [-Server <String>]
   [<CommonParameters>]

Get-ADComputer
   [-AuthType <ADAuthType>]
   [-Credential <PSCredential>]
   [-Identity] <ADComputer>
   [-Partition <String>]
   [-Properties <String[]>]
   [-Server <String>]
   [<CommonParameters>]

Get-ADComputer
   [-AuthType <ADAuthType>]
   [-Credential <PSCredential>]
   -LDAPFilter <String>
   [-Properties <String[]>]
   [-ResultPageSize <Int32>]
   [-ResultSetSize <Int32>]
   [-SearchBase <String>]
   [-SearchScope <ADSearchScope>]
   [-Server <String>]
   [<CommonParameters>]

使用身份参数

Get-AdComputer Identity 该参数用于从 Active Directory 获取单个计算机对象。

您可以使用以下方式识别计算机名称

  • GUID
  • 专有名称
  • SAM 帐户名称
  • 安全标识符 (SID)

让我们考虑一个示例,如果您有一台名为 it-2020 的计算机,并且想要使用 Get-AdComputer 检索它。

Get-AdComputer -Identity "it-2020"

上述 get-adcomputer 命令获取活动目录中由 Identity 参数指定的计算机名称 it-2020

它返回默认的 adcomputer 属性集。

在我们的下一个示例中,如果您想获取给定 it-2020 计算机对象的所有属性,请运行以下命令

Get-AdComputer -Identity "it-2020" -Properties *

在上面的代码中,该命令将获取广告计算机属性,如果要返回一些属性,可以将它们指定为逗号分隔。

酷提示:您知道 Windows 中 cat 命令的等效命令吗?

查找 OU 中的所有计算机

Get-AdComputer 有一个 SearchBase 参数,用于将搜索限制为特定于 OU 或其子 OU。

使用 get-adcomputer searchbase 参数在 PowerShell 中获取 OU 中的所有计算机。

Get-ADComputer -Filter * -SearchBase 'OU=Sales, DC=Shellpro, DC=LOCAL'

上述 PowerShell 获取 adcomputer 过滤器脚本,检索 Active Directory 中特定于组织单位的所有计算机。

上述 PowerShell 脚本的输出获取 OU 中的所有计算机。

[玩转系统] Get-ADComputer- 通过示例查找 OU 中的计算机详细信息

获取 OU 中的计算机列表并将其导出为 CSV

要获取 OU 中的计算机列表并将计算机列表从 Active Directory 导出到 CSV 文件,请使用 PowerShell 脚本

$OULocation = 'ou=Sales,dc=shellgeek,dc=com'
$csvpath = 'C:\powershell\computers_in_ou.csv'
Get-ADComputer -Filter * -SearchBase $OULocation  | Select-object
DistinguishedName,DNSHostName,Name | Export-Csv -NoType $csvpath 

在上面的 get-adcomputer 示例中,我们设置了 OU 位置和 CSV 路径。

Get-AdComputer 过滤器参数使用过滤器限制查询的输出。

它使用 Get-AdComputer SearchBase 参数在特定 OU 中进行搜索。

它列出 OU 中的计算机,并使用 Export-Csv 从活动目录导出计算机列表,并将它们输出到 CSV 文件。

获取名称以特定字符串开头或以特定字符串开头的所有计算机

管理员经常希望获取名称以特定字符串开头的计算机对象。

您可以使用Get-Adcomputer Filter 属性来搜索 AD 计算机对象。

Get-ADComputer -Filter 'Name -like "it-20*"' -Properties *

在上面的 get-adcomputer 示例脚本中,该命令获取名称以 it-20 开头的 ad 计算机属性,并返回计算机对象的所有属性。

酷提示:了解如何使用 userprincipalname 获取 aduser!

获取广告计算机过滤器 - 获取 Active Directory 中的所有计算机

Get-ADComputer -Filter *

在上面的 get-adcomputer 示例中,带有 *(通配符)的 PowerShell get-adcomputer 筛选器返回所有计算机对象。

Get-AdComputer 多个过滤器 - 多个名称

您可以搜索 adcomputer name 或 name 以特定字符串开头的计算机。

Get-ADComputer -Filter 'Name -like "it-20*" -or Name -like "it-21*"' -Properties *

在上面的 get-adcomputer 示例中,它根据 get-adcomputer 类似过滤器名称的属性返回一台或多台名称类似于 it-20 或 it-21 的计算机。

Get-AdComputer 多个过滤器 - 最近 15 天内的 PasswordLastSet

您可以从活动目录中获取计算机名称以过去 15 天内指定的名称和密码更改开头的计算机。

$lastpwddate = [DateTime]::Today.AddDays(-15);
Get-ADComputer -Filter 'Name -like "it-20*" -and PasswordLastSet -ge $lastpwddate ' -Properties *

在上面的 get-adcomputer 示例中,get-adcomputer 过滤名称,如“it-20*”,以获取所有以 it-20 开头的计算机名称以及过去 15 天内的密码更改。

酷提示:了解如何使用 PowerShell 下载 zip 文件!

Windows 2016 操作系统的 Get-AdComputer 过滤器

$os = Get-ADComputer -Filter * -Properties OperatingSystem -ResultPageSize 500
$os | where OperatingSystem -eq "Windows Server 2016" | select name

此命令获取在活动目录中找到的所有不同操作系统。

它使用 get adcomputer 过滤器来获取操作系统等于 Windows Server 2016 的计算机列表。

在上面的示例中,get-adcomputer 过滤操作系统(如果等于 Window Server 2016)并获取所有计算机的列表。

酷提示:使用 set-adgroup 修改 Active Directory 组属性 PowerShell!

使用 Get-AdComputer LDAPFilter 参数

LDAPFilter 参数用于检索一台或多台计算机。

如果您需要在 OU 中查找计算机,让我们考虑一个示例。

Get-ADComputer -LDAPFilter "(name=*it-20*)" -SearchBase "CN=Computers,DC= Sales,DC=com"

此命令从位置 CN= Computers、DC=Sales、DC=com 返回计算机。

Get-AdComputer LDAPFilter 与 SearchBase 参数一起使用。

使用 SearchBase 参数将搜索限制为特定于 OU 或其子 OU。

酷提示:如何删除广告计算机 PowerShell!

获取 AdComputer 序列号

您可以使用 PowerShell 中的 Get-CimInstance cmdlet 获取 Active Directory 计算机序列号,该命令使用 Win32_Bios 方法获取计算机序列号。

 Get-ADComputer -Filter *| Select-Object Name | Foreach-Object {Get-CimInstance Win32_Bios -ComputerName $_.Name -ErrorAction SilentlyContinue | Select-Object PSComputerName,SerialNumber}

在上面的 PowerShell 脚本中,

Get-AdComputer 命令获取所有 Active Directory 计算机。

它将输出传递给第二个命令,在该命令中仅获取 adcomputer 名称。

在下一个命令中,它使用 foreach-object 来迭代 adcomputers。

它使用 Get-CimInstance cmdlet 获取每台 Active Directory 计算机的计算机名称和序列号。

Get-AdComputer 常见问题解答

如何使用 Get-ADComputer -Filter name 在 AD 中查找计算机?

考虑一个示例,要在活动目录中查找计算机名称“comp-1”,请使用以下命令
Get-ADComputer -Filter "name -eq 'comp-1'"

在上面的示例中,如果 get-adcomputer 过滤器名称类似于活动目录中的“comp-1”,则会返回计算机名称。

但是,如果您想使用存储在变量中的计算机名称并找到它
$comp="comp-1"
Get-ADComputer -Filter "name -eq '$comp'"

如何使用PowerShell cmdlet get-adcomputer来过滤操作系统?

如果您想使用 get-adcomputer 过滤器查找所有具有 Windows 10 操作系统的计算机,请使用以下命令

Get-ADComputer -Filter {OperatingSystem -like ‘*Windows 10*’}

在上面的 PowerShell get-adcomputer 示例中,如果 get-adcomputer 筛选操作系统如 ‘*Window 10*’,则该命令将返回计算机列表

如何使用 Get-AdComputer 过滤器查找 OU 中的计算机

要使用 Get-AdComputer 筛选器参数查找 OU(组织单位)中的计算机,请使用以下命令

Get-ADComputer -Filter * -SearchBase“OU=销售,DC=shellgeek,DC=com”

在上面的 PowerShell get-adcomputer 过滤器示例中,如果 get-adcomputer 过滤器类似于 Sales,则会返回 OU 中的计算机列表。

如何使用 Get-AdComputer Searchbase 参数从多个 OU 获取计算机?

让我们考虑一个示例,如果您有多个 OU,并且想要使用 Searchbase 参数从多个 OU 获取计算机列表,请使用以下命令

#Store OUs in variable

$OUs= "OU=Sales,DC=shellgeek,DC=com", "OU=HR,DC=shellgeek,DC=com", "OU=Finance,DC=shellgeek,DC=com"

$Ous | foreach { Get-ADComputer -Filter * -SearchBase $_} | Select Name

使用 foreach 迭代多个 OU,并使用 SearchBase 参数通过 OU 的 get-adcomputer 筛选器获取计算机列表。

如何获取广告计算机上次登录日期?

Get-ADComputer -identity It-2020 -Properties * | FT Name, LastLogonDate -Autosize

此命令返回计算机名称 It-2020get-adcomputer lastlogondate

阅读有关活动目录中的lastlogon 和lastlogontimestamp 之间差异的更多信息。

如何导出 get-adcomputer lastlogondate Export-CSV

让我们考虑一个示例,我们必须查找过去 90 天内未登录 Active Directory 的计算机帐户,您可以使用 LastLogonTimeStamp 获取帐户列表财产。

# 指定时间范围
$inactiveperiod=90

$时间=(获取日期).Adddays(-($inactiveperiod))

Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 1000 -resultSetSize $null -属性名称,SamAccountName|导出 CSV“C:\PowerShell\InActiveComputer.csv” -NoTypeInformation

此命令搜索最后登录时间戳在过去 90 天内未更新的计算机帐户,并将所有过时或不活动的计算机帐户导出到 CSV 文件。

酷提示:检查广告架构版本 PowerShell!

结论

希望您喜欢上面的文章,使用 PowerShell Get-AdComputer cmdlet 从活动目录中查找计算机对象信息。

在上面的PowerShell Get-AdComputer示例中,我们看到了许多不同的示例来根据get-adcomputer过滤器名称或get-adcomputer过滤器操作系统或get-adcomputer过滤器ou获取计算机列表。

上述分步指导如何使用 Get-AdComputer cmdlet 以及不同的参数,例如 IdentityLDAPFilterSearchBase 带有实际示例将帮助您了解 cmdlet 的使用。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

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

取消回复欢迎 发表评论:

关灯