Need a re-name and join domain VBS for Windows 7

Hello all,

I have been wrestling with that issue for a while now. I am setting up Windows 7 images in a corporate environment and need a script to make the imaging process do what Sysprep used to do. I have searched and searched, and I have seen script after script that would do what I need, except that we don't have just one domain. What I am looking for is:

1. Prompt the tech to rename the machine, and then renames it.
2. Prompt the tech to enter the domain to be joined
3. Prompt the tech for a username and password that has rights to rename machines and join domain, and then joins the machine to the domain.

I am VBS challenged, although I see the wisdom in learning it.  I just don't have the time to do it at the moment- I have this project breathing down my neck.

Environment is AD network on 2K3 servers and Win 7 workstations. I'm going to have it run from the Cmd file that runs at the end of the sysprep mini-setup.

Thank for your time!
brcarrollAsked:
Who is Participating?
 
jawa29Commented:
Hi brcarroll

This script is untested, but should do what you are asking.

It prompts for PC Name, Damain Name, Username and Password then renames the PC prior to Joining to the supplied domain using the supplied credentials.

Jawa29
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2

On Error Resume Next

sPCName = InputBox("Please supply Computer Name","Domain Joining Script")
sDomain = InputBox("Please supply Domain Name","Domain Joining Script")
sUserName = InputBox("Please supply a Domain Admin","Domain Joining Script")
sPassword = InputBox("Please supply the password for " & sUserName,"Domain Joining Script")

If sPCName = "" Or sDomain = "" Or sUserName = "" Then
	WScript.Quit
End If

' Rename the PC
Err.Clear
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set cComputers = oWMIService.ExecQuery("Select * from Win32_ComputerSystem")

For Each oComputer in cComputers
	err = oComputer.Rename(sPCName)
Next
If Err.Number <> 0 Then
	WScript.Echo "PC Could not be renamed : (" & Err.Number & ") " & Err.Description
	WScript.Quit
End If

' Join the PC to the Domain
Set oComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2:Win32_ComputerSystem.Name='" & sPCName & "'")

Err.Clear
ReturnValue = oComputer.JoinDomainOrWorkGroup(sDomain, sPassword, sDomain & "\" & sUserName, NULL, JOIN_DOMAIN + ACCT_CREATE)
If Err.Number <> 0 Then
	WScript.Echo "PC Could not be joined to the Domain : (" & Err.Number & ") " & Err.Description
End If

Open in new window

0
 
brcarrollAuthor Commented:
Just to clarify- I've seen lots of scripts where one enters one's domain credentials into the script itself. That's really not an option in my case, as the script will be in the images and will be shipped off all over the company, and used on different domains.
0
 
brcarrollAuthor Commented:
It does put it on the domain- but it throws a "PC Could not be renamed - (1326)" error.

First script I have tried that successfully put a machine on the domain, though. :-)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
brcarrollAuthor Commented:
I looked up 1326- "1326 Logon failure: unknown user name or bad password. " Could that be because I haven't yet presented my credentials to the domain?
0
 
jawa29Commented:
It shouldn't, that's a local change. So as long as the script is run as a local admin it should be fine. On Windows 7 it will need to be run as administrator.
0
 
brcarrollAuthor Commented:
Okay- I'll try elevating privileges with a script that I have for that.

Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = FSO.GetParentFolderName (WScript.ScriptFullName)
If FSO.FileExists(strPath & "\ee.VBS") Then
     objShell.ShellExecute "wscript.exe", _
        Chr(34) & strPath & "\ee.VBS" & Chr(34), "", "runas", 1
Else
     MsgBox "Script file ee.VBS not found"
End If 

Open in new window


-where ee.vbs is what you've written above. Having a struggle getting the XML sysprep file to run properly and people knocking on the door as well, so will test more as soon as I can. Thank you again for your help so far.
0
 
brcarrollAuthor Commented:
Still no luck renaming. Hmm. Will review the configuration on the workstation to see if I've missed anything that might be preventing it from working.
0
 
brcarrollAuthor Commented:
Thank you for your help. Still one hurdle to cross, but I think that's at my end. Thanks again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.