Use VBcript to List Office Users form Active Directory

I have written vbscript to display a list of users from a particler office filtered
by the Office attribute and it is erroring outwith the message below, what is wromg
with thescript below?

D:\download\myscript1.vbs(15, 2) Microsoft VBScript runtime error: Object doesn't
support this property or method: 'objItem.physicalDeliveryOfficeName'

'On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
strComputer = "."

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_UserAccount",,48)

For Each objItem In colItems

      if objItem.physicalDeliveryOfficeName = "BS" Then
            Wscript.Echo objItem.Name + ", " + objItem.FullName
      end if


Wscript.Echo "Done"
Who is Participating?
rogerardConnect With a Mentor Commented:
The code, in and of itself is valid, except for the reference to physicalDeliveryOfficeName, which is not a member of the Win32_UserAccount class.  You fix it by referencing one of the available properties/methods of the Win32_UserAccount class.  See for a list of available attributes.

If you absolutely need to get the physicalDeliveryOfficeName attribute from a user, then you need to query ldap or the global catalog instead.
You will get that error when the entry you're reporting doesn't contain the field you're trying to display. ( ie the field is empty(null) and didn't return with the results)
netimpactAuthor Commented:
It says clearly Object doesn't support this property or method: 'objItem.physicalDeliveryOfficeName'

All the records in our AD are NOT null. Could this be the wrong direction?

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Perhaps a mis-communication?
What I mean is that for the referenced objItem, User1 let say, has no physicalDeliveryOfficeName assigned.  If you go look for User1 in Active directory you'll see that the field for physicalDeliveryOfficeName is blank.

Therefore you'll have an object for User1, but no address.

netimpactAuthor Commented:
Ok how do I fix this code?
netimpactAuthor Commented:
You are right I need to absolutely access the physicalDeliveryOfficeName attribute so I need to query LDAP. Thanks for the tip it was just staring me in the face.
netimpactAuthor Commented:
Not the complete solution
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.