Solved

Modify vbscript - rename computer and join domain

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

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 46

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
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…

809 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