Solved

AD script to change PC name based on GUID in Active Directory

Posted on 2006-07-05
5
400 Views
Last Modified: 2008-01-09
In Active Directory you can assign a PC a GUID when you create it's account.  We are wanting to write a script that looks up the computer name based on this GUID.  We want to do this in case we image a bunch of machines we can run this script to 1) connect to AD 2) lookup the GUID based on the local PC 3) look at the pc name associated with the GUID 4) change the name of the local PC 5) join the pc back to the domain.

Any thoughts would be appreciated!
0
Comment
Question by:CameronFrasnelly
5 Comments
 
LVL 23

Expert Comment

by:TheCleaner
ID: 17046707
You want to change the name to the GUID?  I'm lost I guess.

Are you talking about prestaging a RIS computer?

If you image a bunch of PCs then the GUID will only show up in AD if you pre-stage the computers, in which case you'll be setting the name of the local PC and joining the domain anyway...so I'm not sure what you are asking, unless you are wanting something that does the "prestaging" automatically.
0
 

Author Comment

by:CameronFrasnelly
ID: 17050997
: ) We want to lookup the PC name based on the netbootGUID in AD.  So first we'll retrieve the local UUID (which we will have prestaged as the netbootGUID in AD) then we'll lookup the computer in AD that has the same GUID set for it's netboodGUID and grab the computer name.  At that point we'll rename the pc and join the domain and life will be good! : )
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 17051960

Isn't at least part of that covered by this:

http://support.microsoft.com/kb/302467

As far as I've seen the netbootGUID is only completed if the machine is installed by RIS (for example). I know very little about it beyond that. Searching AD for things that that isn't too bad, however it's an octetString so it may need converting depending on exactly how you would go about comparing them.

This snippet of VbScript is what I've used in the past to read and convert the objectGUID into a useful format, as with the netbootGUID it's an octetString:

Option Explicit

' Global Constants

Const ADS_SCOPE_SUBTREE = 2

' Global Variables

Dim objRootDSE, objFileSystem, objFile
Dim strDomainName

'
' Subroutines
'

Sub FindObject

      Dim objConnection, objCommand, objRecordSet
      Dim strUserName, strUserSID
      Dim arrUserSID
      Dim i

      Set objConnection = CreateObject("ADODB.Connection")
      objConnection.Provider = "ADsDSOObject"
      objConnection.Open "Active Directory Provider"

      Set objCommand = CreateObject("ADODB.Command")
      objCommand.ActiveConnection = objConnection

      objCommand.CommandText = "SELECT name, objectSID " &_
            "FROM 'LDAP://" & strDomainName & "' WHERE objectClass='user'"

      objCommand.Properties("Page Size") = 1000
      objCommand.Properties("Timeout") = 600
      objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
      objCommand.Properties("Cache Results") = False

      Set objRecordSet = objCommand.Execute

      While Not objRecordSet.EOF
            On Error Resume Next
            strUserName = objRecordSet.Fields("name")
            arrUserSID = objRecordSet.Fields("objectSID")

            ' Convert the SID from a Bit Array to a Hexadecimal String

            strUserSID = ""
            For i = LBound(arrUserSID) To UBound(arrUserSID)
                  strUserSID = strUserSID & Hex(AscB(MidB(arrUserSID, i + 1, 1)) \ 16) &_
                        Hex(AscB(MidB(arrUserSID, i + 1, 1)) Mod 16)
            Next

            WScript.Echo strUserName & VbTab & strUserSID
            On Error Goto 0

            objRecordSet.MoveNext
      Wend

      objConnection.Close

      Set objRecordSet = Nothing
      Set objCommand = Nothing
      Set objConnection = Nothing
End Sub


'
' Main Code
'

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.OpenTextFile("Computers.txt", 2, True, 0)

Set objRootDSE = GetObject("LDAP://RootDSE")
strDomainName = objRootDSE.Get("defaultNamingContext")

FindObject

Set objRootDSE = Nothing
Set objFile = Nothing
Set objFileSystem = Nothing
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The HP utility "HP Lights-Out Online Configuration Utility for Windows Server 2003/2008" could be of great use when it comes to remotely configure a HP servers ILO WITHOUT rebooting the server. We would only need to create and run scripts using thi…
Scenerio: You have a server running Server 2003 and have applied a retail pack of Terminal Server Licenses.  You want to change servers or your server has crashed and you need to reapply the Terminal Server Licenses. When you enter the 16-digit lic…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question