Solved

Modify vbscript - rename computer and join domain

Posted on 2012-03-17
12
1,970 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 47

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 47

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:ivanoviola
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 500 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:ivanoviola
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:ivanoviola
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:ivanoviola
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:ivanoviola
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

680 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