Solved

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

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

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Group Policy & Netlogin Services 5 58
Auto-Enrollment Group Policy 2 60
Moving RDP Server to New Server. 3 73
Robocopy Doesn't Retain Shared Folders After Copying 5 82
So you have two Windows Servers and you have a directory/folder/files on one that you'd like to mirror to the other?  You don't really want to deal with DFS or a 3rd party solution like Doubletake. You can use Robocopy from the Windows Server 200…
Organizations create, modify, and maintain huge amounts of data to help their businesses earn money and generally function.  Typically every network user within an organization has a bit of disk space to store in process items and personal files.   …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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