Solved

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

Posted on 2006-07-05
5
396 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I guess it is not common knowledge to most Wintel engineers/administrators: If you have an SNMP-based monitoring system in your environment (and it's common to have SNMP or Syslog) it's reasonably easy to enable monitoring of the Windows Event logs,…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now