Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2133
  • Last Modified:

Modify vbscript - rename computer and join domain

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
Ivano Viola
Asked:
Ivano Viola
  • 5
  • 5
  • 2
1 Solution
 
Martin LissRetired ProgrammerCommented:
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
 
Martin LissRetired ProgrammerCommented:
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
 
RobSampsonCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Ivano ViolaSystem AdministratorAuthor Commented:
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
 
RobSampsonCommented:
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
 
Ivano ViolaSystem AdministratorAuthor Commented:
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
 
Ivano ViolaSystem AdministratorAuthor Commented:
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
 
RobSampsonCommented:
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
 
Ivano ViolaSystem AdministratorAuthor Commented:
This is the error:
Error
0
 
RobSampsonCommented:
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
 
Ivano ViolaSystem AdministratorAuthor Commented:
Error = 2202

Thanks Rob!
0
 
RobSampsonCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now