powershell - 针对 Active Directory 运行查询的 Powershell 脚本
问题描述
我有以下 Powershell 脚本:
$domainObj = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$PDC = ($domainObj.PdcRoleOwner).Name
$SearchString = "LDAP://"
$SearchString += $PDC + "/"
$DistinguishedName = "DC=$($domainObj.Name.Replace('.', ',DC='))"
$SearchString += $DistinguishedName
$Searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]$SearchString)
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$Searcher.SearchRoot = $objDomain
$Searcher.filter="samAccountType=805306368"
$Result = $Searcher.FindAll()
Foreach($obj in $Result)
{
Foreach($prop in $obj.Properties)
{
$prop
}
Write-Host "------------------------"
}
我需要对此进行修改以执行以下操作,但我不确定如何应用正确的过滤器,我认为需要进入$Searcher.filter
:
- 将脚本更改为仅返回 Domain Admins 组的成员。
- 更改脚本以返回域中的所有计算机。
- 添加过滤器以仅返回运行 Windows 10 的计算机。
解决方案
您可以执行以下操作:
$domainObj = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$PDC = ($domainObj.PdcRoleOwner).Name
$searchString = "LDAP://{0}/DC={1}" -f $PDC,$domainObj.Name.Replace('.', ',DC=')
$Searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]$SearchString)
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$Searcher.SearchRoot = $objDomain
# Domain Admins
$Searcher.filter='samAccountName=Domain Admins'
$DAs = $Searcher.FindAll().Properties.Member
# All Computers
$Searcher.filter='objectClass=Computer'
$Computers = $Searcher.FindAll()
# Windows 10
$Computers | Where {
$_.Properties.OperatingSystemVerison -match '^10\D' -and $_.Properties.OperatingSystem -notmatch 'Server'
}
推荐阅读
- excel - 隐藏行的宏不会隐藏每一行
- php - 每个对象的最低计数
- c - msvc中的按位运算左移与linux gcc不同?
- docker - 为什么不能从浏览器访问我修改后的 docker elasticsearch 副本?
- python - 使用python从所有用户的mongodb数据库中获取所有电子邮件的功能
- php - 字符串多次转换为数组
- google-sheets - ARRAYFORMULA - 不适用于连接
- css - Woocommerce:如何制作 woocommerce_form_field 内联块
- c# - 如何在 xamarin 中绑定 DataGrid?
- git - 我的 git repo 怎么能有一个没有主 ref 的主?