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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.