?
Solved

Modify vbscript - rename computer and join domain

Posted on 2012-03-17
12
Medium Priority
?
2,070 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:Ivano Viola
[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
  • 5
  • 2
12 Comments
 
LVL 49

Expert Comment

by:Martin Liss
ID: 37735412
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 49

Expert Comment

by:Martin Liss
ID: 37735506
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
ID: 37735758
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Author Comment

by:Ivano Viola
ID: 37735902
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 2000 total points
ID: 37735970
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:Ivano Viola
ID: 37735983
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
 
LVL 21

Author Comment

by:Ivano Viola
ID: 37736021
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
ID: 37736077
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:Ivano Viola
ID: 37736120
This is the error:
Error
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37736231
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:Ivano Viola
ID: 37736313
Error = 2202

Thanks Rob!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37736317
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

752 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