?
Solved

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

Posted on 2006-07-05
5
Medium Priority
?
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 71

Accepted Solution

by:
Chris Dent earned 2000 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

by Batuhan Cetin In this article I will be guiding through the process of removing a failed DC metadata from Active Directory (hereafter, AD) using the ntdsutil tool in a Windows Server 2003 environment. These steps are not necessary in a Win…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

800 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