Solved

Modify vbscript - rename computer and join domain

Posted on 2012-03-17
12
1,843 Views
Last Modified: 2012-03-20
I have the following code which renames a computer then joins it to Active Directory. I would like some help to make the following changes:

1. Is there anyway way to hide the cancel button on the input box (just have the OK button)? If not can we make the cancel button loop back to the input box? I don't want the script to be canceled.

2. After a computer name is input, I would like the script to search AD to see if the computer name already exists. If the computer already exists, the script should reset the computer object so the computer can be re-added to AD.

I appreciate your help like always.

IV

Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2

Dim sCmpName
Dim sUser, sPassword, sDomain, sOU

sUser = "Username"
sPassword = "Password"
sDomain = "MyDomain" 
sOU = "OU Path"

sCmpName = InputBox("Enter the new computer name:", "Computer Name")
If sCmpName = "" Then
Wscript.Echo "Exiting script."
Wscript.Quit
End If

Dim oWMI, oCmp, oOS, sReturn

Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
For Each oCmp in oWMI.InstancesOf("Win32_ComputerSystem")
sReturn = oCmp.Rename(sCmpName)
If sReturn <> 0 Then
Wscript.Echo "Rename failed. Error = " & Err.Number & _
vbcrlf & "Exiting script."
Else
Wscript.Echo "Rename successful."
sReturn = oCmp.JoinDomainOrWorkgroup(sDomain, sPassword, _
sDomain & "\" & sUser, sOU, JOIN_DOMAIN)
If sReturn <> 0 Then
Wscript.Echo "Join domain failed. Error = " & Err.Number & _
vbcrlf & "Exiting script."
Else
Wscript.Echo "Join domain successful."
End If
Wscript.Echo "Rebooting computer..."
Dim oShell

Set oShell = CreateObject("Wscript.Shell")
sReturn = oShell.Run("%comspec% /c shutdown -r -t 0 -f")

End If
Next

Open in new window

0
Comment
Question by:ivanoviola
  • 5
  • 5
  • 2
12 Comments
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
To check for the cancel do this


sCmpName = InputBox("Enter the new computer name:", "Computer Name")

If StrPtr(sCmpName) = 0 Then
    MsgBox "You pressed cancel!"
End If

If sCmpName = "" Then
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Oh and if you want to not have a Cancel button at all, then create a UserForm that looks like an InputBox without the Cancel button.
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
For part 1, change this
sCmpName = InputBox("Enter the new computer name:", "Computer Name")
If sCmpName = "" Then
Wscript.Echo "Exiting script."
Wscript.Quit
End If

Open in new window


to this:
sCmpName = ""
While sCmpName = ""
sCmpName = Trim(InputBox("Enter the new computer name:", "Computer Name"))
Wend

Open in new window


For part 2, do you want to delete the computer object so that it can be re-added?  This seems like a bit of a risk to me....what message do you get when you use the existing code to add an existing computer?

Regards,

Rob.
0
 
LVL 21

Author Comment

by:ivanoviola
Comment Utility
Hi Rob,

I've yet to try the code (I will try it tomorrow). I'm betting that the script will fail because the computer account already exists in AD. I'm thinking either resetting the computer object or deleting it will work.

Thanks for the help.

IV
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
Comment Utility
OK, add this constant:
Const DOMAIN_JOIN_IF_JOINED   = 32

Then change
sReturn = oCmp.JoinDomainOrWorkgroup(sDomain, sPassword, _
sDomain & "\" & sUser, sOU, JOIN_DOMAIN)

to
sReturn = oCmp.JoinDomainOrWorkgroup(sDomain, sPassword, _
sDomain & "\" & sUser, sOU, JOIN_DOMAIN + ACCT_CREATE + DOMAIN_JOIN_IF_JOINED)

to have it join the computer even if it is already joined.

Regards,

Rob.
0
 
LVL 21

Author Comment

by:ivanoviola
Comment Utility
Rob,

I give that a try. What will happen if I have the hard-coded OU path in the script and the computer is already joined to the domain in a different OU?

IV
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 21

Author Comment

by:ivanoviola
Comment Utility
Rob,

I tried the code and the rename worked fined. I ran in to a small problem.
The test computer was already a member of the domain. The name for the test computer was renamed in AD and local but I got the following error:
domain join error = 0
I was still able to access the computer on the domain using the new name.

This script will be used using sysprep. The computers will be in a workgroup before this script is run. Would it be better just to delete the computer object from AD before adding it to the domain?

IV
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
What was the exact error message you got?   A return code of zero suggests there is no error, and that the command completed successfully, but the code should handle that, and not show "error".....
0
 
LVL 21

Author Comment

by:ivanoviola
Comment Utility
This is the error:
Error
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Oh I see.  Can you change line 31 from this:
Wscript.Echo "Join domain failed. Error = " & Err.Number & _

to this
Wscript.Echo "Join domain failed. Error = " & sReturn & _

so that we can see that actual return code?

Thanks,

Rob.
0
 
LVL 21

Author Comment

by:ivanoviola
Comment Utility
Error = 2202

Thanks Rob!
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Hmmm, I can't find much information on that error code, but one I did see was that it could mean "The specified username is invalid"

Are you sure your domain credentials are correct?

Rob.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

15 Experts available now in Live!

Get 1:1 Help Now