理解 AD 请求
understanding with AD requests
所以,我想用 AD 做点什么。
获取我广告的特定文件夹中每个元素的每个属性。
这是我目前的代码,我也不太明白。
$strDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $strDomain
$objSearcher.PageSize = 100
$objSearcher.SearchScope = "subtree"
# Specify attribute values to retrieve.
$arrAttributes = @("distinguishedName","sn","givenname")
ForEach($strAttribute In $arrAttributes)
{
$objSearcher.PropertiesToLoad.Add($strAttribute) > $Null
}
# Filter on object with specified Common Name.
$objSearcher.Filter = "(&(objectClass=user)(CN=*))"
$colResults = $objSearcher.FindAll()
ForEach ($strResult In $colResults)
{
$strDN = $strResult.Properties.Item("distinguishedName") + "," + $strResult.Properties.Item("givenname")
Write-Host $strDN
}
这个问题是过滤器不像我认为的那样工作,并且在 return 中放置 CN=* 以外的任何内容都没有任何作用。
第二个问题是它为我提供了 AD 中的所有内容。甚至非用户
我注意到一条路径,想知道我是否可以使用它来访问该文件夹,然后使用“foreach”
但路径看起来像这样:
CN="user",OU="folder I want",OU="parent folder",DC,DC,DC
PS:出于安全原因,我无法给出实际路径,因为我不知道它的“价值”是多少,也不想冒险。
但我的问题有两点:
- 有多个“OU”和“DC”
- 它与通常的路径相反(就像文件浏览器一样)
所以有没有一种方法可以定位我的 OU="我想要的文件夹" 并提取其用户?
你能解释一下它在这个过程中是如何工作的吗?
谢谢。
要获取特定 OU(您称之为文件夹)内所有用户的属性,您所要做的就是
Import-Module ActiveDirectory
# enter the DistinguishedName of the OU in which you want to search for users
$UsersOU = 'OU=FolderYouWant,OU=Users,DC=YourDomain,DC=com'
# Get-ADUser returns by default these properties
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
# so in this case that is more than enough. If you want extra properties
# just ask for them using parameter `-Properties`
$result = Get-ADUser -Filter * -SearchRoot $UsersOU
# output on screen
$result | Format-Table -AutoSize
# you can limit what you want to see, for instance
$result | Format-Table -Property DistinguishedName, GivenName, Surname
# or write this to a CSV file you can open in Excel
$result | Export-Csv -Path 'D:\Test\ADusers.csv' -UseCulture -NoTypeInformation
对于要搜索的 OU 的 DistinguishedName,打开 ADUC,右键单击 OU select 属性并向下滚动直到看到 属性 DistinguishedName
。双击并复制。
P.S。 DistinguishedName 始终是相反的,因为它不是您习惯使用 Windows 文件夹的路径格式。 (CanonicalName 更像那个)
P.S.2 如果您知道要在其中搜索的 OU 名称,但不知道 DistinguishedName,则可以使用:
$UsersOU = (Get-ADOrganizationalUnit -Filter 'Name -like "TheNameOfTheOUtheUsersAreIn"').DistinguishedName
所以,我想用 AD 做点什么。
获取我广告的特定文件夹中每个元素的每个属性。 这是我目前的代码,我也不太明白。
$strDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $strDomain
$objSearcher.PageSize = 100
$objSearcher.SearchScope = "subtree"
# Specify attribute values to retrieve.
$arrAttributes = @("distinguishedName","sn","givenname")
ForEach($strAttribute In $arrAttributes)
{
$objSearcher.PropertiesToLoad.Add($strAttribute) > $Null
}
# Filter on object with specified Common Name.
$objSearcher.Filter = "(&(objectClass=user)(CN=*))"
$colResults = $objSearcher.FindAll()
ForEach ($strResult In $colResults)
{
$strDN = $strResult.Properties.Item("distinguishedName") + "," + $strResult.Properties.Item("givenname")
Write-Host $strDN
}
这个问题是过滤器不像我认为的那样工作,并且在 return 中放置 CN=* 以外的任何内容都没有任何作用。 第二个问题是它为我提供了 AD 中的所有内容。甚至非用户
我注意到一条路径,想知道我是否可以使用它来访问该文件夹,然后使用“foreach” 但路径看起来像这样:
CN="user",OU="folder I want",OU="parent folder",DC,DC,DC
PS:出于安全原因,我无法给出实际路径,因为我不知道它的“价值”是多少,也不想冒险。
但我的问题有两点:
- 有多个“OU”和“DC”
- 它与通常的路径相反(就像文件浏览器一样)
所以有没有一种方法可以定位我的 OU="我想要的文件夹" 并提取其用户? 你能解释一下它在这个过程中是如何工作的吗?
谢谢。
要获取特定 OU(您称之为文件夹)内所有用户的属性,您所要做的就是
Import-Module ActiveDirectory
# enter the DistinguishedName of the OU in which you want to search for users
$UsersOU = 'OU=FolderYouWant,OU=Users,DC=YourDomain,DC=com'
# Get-ADUser returns by default these properties
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
# so in this case that is more than enough. If you want extra properties
# just ask for them using parameter `-Properties`
$result = Get-ADUser -Filter * -SearchRoot $UsersOU
# output on screen
$result | Format-Table -AutoSize
# you can limit what you want to see, for instance
$result | Format-Table -Property DistinguishedName, GivenName, Surname
# or write this to a CSV file you can open in Excel
$result | Export-Csv -Path 'D:\Test\ADusers.csv' -UseCulture -NoTypeInformation
对于要搜索的 OU 的 DistinguishedName,打开 ADUC,右键单击 OU select 属性并向下滚动直到看到 属性 DistinguishedName
。双击并复制。
P.S。 DistinguishedName 始终是相反的,因为它不是您习惯使用 Windows 文件夹的路径格式。 (CanonicalName 更像那个)
P.S.2 如果您知道要在其中搜索的 OU 名称,但不知道 DistinguishedName,则可以使用:
$UsersOU = (Get-ADOrganizationalUnit -Filter 'Name -like "TheNameOfTheOUtheUsersAreIn"').DistinguishedName