Solved

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

23 Experts available now in Live!

Get 1:1 Help Now