Current location: Hot Scripts Forums » Programming Languages » Visual Basic » check to see if a computer already exists in AD


check to see if a computer already exists in AD

Reply
  #1 (permalink)  
Old 04-03-09, 03:30 PM
cyr0n_k0r cyr0n_k0r is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
check to see if a computer already exists in AD

Here is what I am trying to do.
A script will run after our sysprep image during the "runonce" phase of setup. The computer is imaged with a random computer name and joined to a workgroup. It will preform the following steps.

prompt for AD username
prompt for AD password
search AD to see if a GUID already exists for this computer
If GUID is found present a box saying "this computer was found in AD as %computername%, do you wish to keep this name?"
If yes, rename computer to matching AD computer name and join domain.
If no/or no computer GUID was found, prompt for what you want to call this computer then take that input and add to domain.

I have most of the code already, but the one thing I don't know how to do is search AD for a computer by GUID (or at all)

The problem I think will be that the computer currently has a random generated computer name after it's imaged, so I'm not sure how to search AD to see if it used to exist. Lets say the computer used to be called "finance34" but has since been reimaged and now it is on a workgroup with a random computer name of "winnt-22947387". How would I search AD to see if the computer USED to be called something even though it isn't currently called finance34? I assume the best way would be to look for the GUID, but if someone has a better suggestion that would be great.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-07-09, 11:39 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,167
Thanks: 3
Thanked 8 Times in 8 Posts
Perhaps this will help (I did not write this code):

Code:
'Enable explicit variable declaration
'Option Explicit
Const ADS_SCOPE_SUBTREE = 2
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144
 
title = "Notebook Config"
strDomain = InputBox("Enter your domain", title & " - Enter Credentials", "domain.internal")
strUser = InputBox("Enter a username with administrative privelages on the " & strDomain & " domain", title & " - Enter Credentials", "administrator")
strPassword = InputBox("Enter password for " & strUser, title & " - Enter Credentials")
 
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
 
Set objWMIService = GetObject("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
 
message = "Please enter computer name. Leave blank or press cancel to quit."
newComputerName = InputBox(message, title & " - New Computer Name", "NB")
 
If newComputerName = "" Then
 WScript.quit
End If
 
If isComputerAccountExists(newComputerName) = True Then
 deleteaccount(newComputerName)
 MsgBox "the newcomputer name you enter found and deleted"
End If
 
areYousure = MsgBox("Are you sure you want to add computer to domain with name:" & vbCrLf & vbCrLf & newComputerName, vbYesNo + vbQuestion, "Add computer to domain")
 
If areYouSure = "7" Then
 MsgBox "Exiting script.", vbInformation
 WScript.quit
End If 
 
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
strComputer & "'")
 
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, "ou=ou1,ou=ou2,ou=ou3,ou=ou4,ou=ou5,dc=domain,dc=internal", _
JOIN_DOMAIN + ACCT_CREATE)
        
If ReturnValue = 0 Then
 MsgBox "Computer added to the " & strDomain & " domain without error. Proceeding to change computer name..."
Else
 MsgBox "Computer not added to the " & strDomain & " domain successfully. Return value: " & ReturnValue
End If
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colComputers = objWMIService.ExecQuery _
( "Select * from Win32_ComputerSystem" )
 
 
For Each objComputer In colComputers
 MsgBox "Renaming computer to: " & newComputername
 ErrCode = objComputer.Rename(newComputerName, strPassword, strUser)
 If ErrCode = 0 Then
  Set WSHShell = WScript.CreateObject("WScript.Shell")
  msgShutdown = WshShell.Popup("Computer renamed correctly. Computer will restart in 5 seconds.", 4, "Restart")
  WshShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 1"
 Else
  MsgBox "Error changing computer name. Error code: " & ErrCode
 End If  
Next 
 
Sub deleteaccount(Computer)
 Set objConnection = CreateObject("ADODB.Connection")
 Set objCommand = CreateObject("ADODB.Command")
 objConnection.Provider = "ADsDSOObject"
 objConnection.Open "Active Directory Provider"
  
 Set objCommand.ActiveConnection = objConnection
 objCommand.CommandText = "Select ADsPath From " & _
 "'LDAP://DC=domain,DC=com' Where objectClass='computer'" & _
 " and Name = '" & computer & "'"  
 objCommand.Properties("Page Size") = 1000
 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 Set objRecordSet = objCommand.Execute
 objRecordSet.MoveFirst
  
 Do Until objRecordSet.EOF
  Set objComputer = GetObject(objRecordSet.Fields("ADsPath").Value)
  objComputer.DeleteObject(0)
  objRecordSet.MoveNext
 Loop
End Sub
 
Function isComputerAccountExists(computer)
 Dim conn, cmd, rs
 Set conn = CreateObject("ADODB.Connection")
 Set cmd = CreateObject("ADODB.Command")
 conn.provider = "adsdsoobject"
 conn.open "active directory provider"
 cmd.activeconnection = conn
 cmd.commandtext = "<LDAP://" & GetObject("LDAP://rootdse").Get("defaultnamingcontext") & ">;(&(objectcategory=computer)(objectclass=computer)(cn=" & computer & "));cn;subtree"
 Set rs = cmd.Execute
 If rs.recordcount = 0 Then
  isComputerAccountExists = False
 Else
  isComputerAccountExists = True
 End If
End Function
Pete
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if username or email already exists zitwep PHP 4 10-13-10 10:13 AM
[SOLVED] Check if a directory exists, If not create it xxvatarxx PHP 9 04-04-08 07:27 AM
Check If File exists on network mattdc2001 PHP 11 06-15-07 05:28 PM
[SOLVED] check if directory exists lordy PHP 2 07-04-05 02:17 AM
how do i check to see if something already exists in a column in a DB tisza PHP 2 01-06-04 07:54 PM


All times are GMT -5. The time now is 01:44 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.