question

DavidZemdegs avatar image
0 Votes"
DavidZemdegs asked RichMatheisen-8856 commented

DirectorySearcher and custom AD attributes

Greetings,
I am using the directorysearcher to retrieve the value of a custom AD attribute.
I am at a loss to explain this behaviour when I try to retrieve it.
I first run this code:

 $searcher = New-Object DirectoryServices.DirectorySearcher
 $searcher.Filter = "(&(name=Computer1)(objectClass=computer))"
 $searcher.pageSize = 1000
 $ADSearchResults = $searcher.FindAll()

I then try to display the custom attribute by running this command:

 $ADSearchResults.Properties.CustomReboot

...and nothing is displayed.
If I type this:

 $ADSearchResults.Properties.CustomReboot[0]

...then the value is returned - however if I type this:

 $ADSearchResults.Properties

...all the properties are returned - now here's the thing - if I now type this:


 $ADSearchResults.Properties.CustomReboot

It returns a value where it didnt when I first typed it after the findall command.
Why is this happening?

Thanks
David Z

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is the CustomReboot property returned if you run $searcher.FindAll() | select -ExpandProperty properties?

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 commented

Add "$searcher.PropertiesToLoad = 'CustomReboot'".

PowerShell makes life easy, but if you're dealing with .Net you have to do the work. ;-)

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried that as well but the behaviour I described remains the same.

0 Votes 0 ·

I'm not familiar with the property "CustomReboot". In the AD schema, what's the value of the isSingleValued property for the attribute?

Do you get the results you expect if you do this: $ADSearchResults.Get("CustomReboot")

Not to change the subject, but why do you want to go to all that trouble to avoid using Get-ADComputer computer1 -properties CustomReboot? A long time ago we had to use ADSI, but for something like this I don't see any advantage in continuing to do so.




0 Votes 0 ·
DavidZemdegs avatar image DavidZemdegs RichMatheisen-8856 ·

This is a custom AD attribute that we have created.
It is not multivalued.
I cannot use get-adcomputer as this script runs on every computer in our organisation and we do not intend to install RSAT everywhere.
If I try $ADSearchResults.Get("CustomReboot") I get an error saying
[system.directoryservices.searchresult] does not contain a method called 'get'

0 Votes 0 ·
Show more comments
DavidZemdegs avatar image
0 Votes"
DavidZemdegs answered

Now the behaviour is getting weirder.
I tried retrieving the value for computer A and
$ADSearchResults.Properties.CustomReboot[0] didnt work where
$ADSearchResults.Properties.CustomReboot did.

but for computer B
$ADSearchResults.Properties.CustomReboot didnt work where
$ADSearchResults.Properties.CustomReboot[0] did.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.