Find computer location from Active Directory

All computers in my organization are provided with a value in the Location-field of the computer object in AD. The values are FirstFloorNorth, FirstFloorMid, FirstFloorSouth, ThirdFloorNorth or ThirdFloorMid.

Now I want to set the default printer depending on the computers location value.

Any ideas?


Regards
Kasper K
Kasper KatzmannSeniorkonsulentAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rutten-dConnect With a Mentor Commented:
this should work:

'*********************************************************************************

Option Explicit
Dim objComputer , objPrinter , objShell , strComputername , strLocation , strDomain

Set objShell = CreateObject("WScript.Shell")
Set objPrinter = CreateObject("WScript.Network")

'get domain and computername from Windows environment:
strDomain = objShell.ExpandEnvironmentStrings("%USERDOMAIN%")
strComputername = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

'bind to the computer object :
Set objComputer = GetObject("WinNT://" & strDomain & "/" & strComputername & ",computer")

'set default printer depending on Location value in AD:

if objComputer.location = "" then wScript.Quit
      
if objComputer.location =  "FirstFloorNorth" then
      objPrinter.SetDefaultPrinter "\\ServerName\Printer1"
end if

if objComputer.location = "FirstFloorMid" then
      objPrinter.SetDefaultPrinter "\\ServerName\Printer2"
end if
      
if objComputer.location = "FirstFloorSouth" then
      objPrinter.SetDefaultPrinter "\\ServerName\Printer3"
end if
      
if objComputer.location = "ThirdFloorNorth" then
      objPrinter.SetDefaultPrinter "\\ServerName\Printer4"
end if

if objComputer.location = "ThirdFloorMid" then
      objPrinter.SetDefaultPrinter "\\ServerName\Printer5"
end if
'*********************************************************************************

Of course you'll have to replace the UNC-paths to the printers with the real ones.

On my pc it gives me an error "The directory property cannot be found in the cache" when querying for the value of "location" , however
I'm fairly sure the script is fine and think that maybe I have an issue in my testlab.

greets
0
 
Kasper KatzmannSeniorkonsulentAuthor Commented:
I get the same error :-(
0
 
JackOfPHCommented:
listening
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Kasper KatzmannSeniorkonsulentAuthor Commented:
I'm not sure, but could it be that the location attribute are not supported by WinNT and that LDAP is the answer?

If that's the case, is it possible to rewrite the script?

/Kasper K
0
 
rutten-dCommented:
i tried the LDAP provider , same error....

I'm at a loss , this should work
0
 
Kasper KatzmannSeniorkonsulentAuthor Commented:
Yippee-ki-yay

I've got the solution! I combined ruten-d's solution with one i found in a newsgroup.
It's not nice coding but anyway here it comes.

If anyone wants to clean it up, then go ahead. I don't know how myself.

'*************************************************
Const ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")


Const ADS_SCOPE_SUBTREE = 2

Set objShell = CreateObject("WScript.Shell")
Set objPrinter = CreateObject("WScript.Network")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
strComputername = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, Location from 'LDAP://DC=MyDomain,DC=COM' Where objectClass='computer' AND Name = '" & strComputername & "'"

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst



'set default printer depending on Location value in AD:

if objRecordSet.Fields("location").Value = "" then wScript.Quit
     
if objRecordSet.Fields("location").Value =  "FirstFloorNorth" then
     objPrinter.SetDefaultPrinter "\\ServerName\Printer1"
end if

if objRecordSet.Fields("location").Value = "FirstFloorMid" then
     objPrinter.SetDefaultPrinter "\\ServerName\Printer2"
end if
     
if objRecordSet.Fields("location").Value = "FirstFloorSouth" then
     objPrinter.SetDefaultPrinter "\\ServerName\Printer3"
end if
     
if objRecordSet.Fields("location").Value = "ThirdFloorNorth" then
     objPrinter.SetDefaultPrinter "\\ServerName\Printer4"
end if

if objRecordSet.Fields("location").Value = "ThirdFloorMid" then
     objPrinter.SetDefaultPrinter "\\ServerName\Printer5"
end if

'*************************************************
0
 
rutten-dCommented:
great , thanks for the update
always nice to learn
0
All Courses

From novice to tech pro — start learning today.