Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Need a script to update the location of each computer to the location tab

Hi,

I need a script which can update the computers location.

I have this in a text file.
Machinename : Location of the computer

Is there a way to do this.
Need a report to find if it has updated or not.
Regards
Sharath
Avatar of richy92
richy92
Flag of United States of America image

maybe could use ldifde and the modify parameter to change the location in active directory. The text file would need to contain more than just the location though and it must be formatted in a certain way. There is also a util called DSMOD which may be easy to use
I found this site helpful. http://www.computerperformance.co.uk/Logon/CSVDE_LDIFDE.htm
Avatar of RobSampson
Are you talking about the AD Location attribute?  Or a local computer attribute (that I'm not aware of)?

Regards,

Rob.
Avatar of bsharath

ASKER

In AD > Computers > Location...
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi RobSampson

In what format should this file be.

ComputerLocations.txt"
As you specified, a text file with:

Machinename:Location
Machinename:Location

For example,
WORKPC1:City1
WORKPC2:City2

Regards,

Rob.
This works great and you have given me a bonus of even updating the old location as this is soooo useful to me....

Thanks a lot...
One more help Rob.In this same script can you remove the updating part and just give me a way to output the current status in the computers > location.After updating the location with your script.Later i would want to take a report of what is there when ever i require without updating....

I can raise a new Q if you say ok....
Sorry Sharath, i don't follow with that last bit.....do you want to remove the visible output "while" it is processing, and just leave the "Processed" file as is, or do you want to change the "processed" file?

Regards,

Rob.
I want to fetch only the content in the location tab in all machines in the network.

Now the script updates and even shows the old content.What i want is just to show the content

Like i want to know
Machinename  and  Location content to a csv file...
I see.  Do you want this to happen from the machines in a text file, or the entire AD?

Rob.
Machines in the text file...
Oh, well, that's pretty easy, given what we already have.  I have modified it just to output the current Location only, without changing anything.

'================
strInputFile = "Computers.txt"
strOutputfile = "ComputerLocations_Current.csv"

If Right(LCase(WScript.FullName), 11) = "wscript.exe" Then
      Set objShell = CreateObject("WScript.Shell")
      objShell.Run "cscript """ & WScript.ScriptFullName & """", 1, False
      Set objShell = Nothing
      WScript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1

Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

 ' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

strDetails = """Machine Name"",""Current Location"""

While Not objInputFile.AtEndOfStream
      strLine = objInputFile.ReadLine
      strComputer = strLine
      
      'strFilter = "(&(objectCategory=person)(objectClass=user))"
      strFilter = "(&(objectClass=computer)(cn=" & strComputer & "))"
      
      ' Comma delimited list of attribute values to retrieve.
      'strAttributes = "sAMAccountName,cn"
      strAttributes = "cn,Location"
      
      ' Construct the LDAP syntax query.
      strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
      adoCommand.CommandText = strQuery
      adoCommand.Properties("Page Size") = 100
      adoCommand.Properties("Timeout") = 30
      adoCommand.Properties("Cache Results") = False
      
      ' Run the query.
      Set adoRecordset = adoCommand.Execute
      
      ' Enumerate the resulting recordset.
      Do Until adoRecordset.EOF
          ' Retrieve values and display.
            strComputerName = Replace(adoRecordset.Fields("cn").Value, "CN=", "")
            WScript.Echo "Processing " & strComputer
            strDetails = strDetails & VbCrLf & """" & strComputerName & """,""" & adoRecordset.Fields("Location") & """"
          ' Move to the next record in the recordset.
          adoRecordset.MoveNext
      Loop
      
      ' Clean up.
      adoRecordset.Close
      Set adoRecordset = Nothing
Wend

adoConnection.Close

Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.Write strDetails
objOutputFile.Close
Set objOutputFile = Nothing

WScript.Echo "Done"
MsgBox "Done"
'================

Regards,

Rob.
Thanks a lot this worked.I have raised a new Q for this Answer please put the answer there.I shall close it.

https://www.experts-exchange.com/questions/22781531/Fetch-the-machine-location-details-to-a-csv-file.html


Wanted to give you more points for the excellent job...:)