Link to home
Start Free TrialLog in
Avatar of snafumaster
snafumasterFlag for United States of America

asked on

Computer and Workgroup Name via Script & InputBox

I have been tasked to come up with a solution where a person with no computer skills can rename the local machine and workgroup.  I have offered to train, educate, create tutorials, and a few other "teach a man to fish" options.  None were accepted, so I need to give a fish.  In going over the criteria, I know the client wants the following flow:
No computer skills required
Starts on initial boot
Simple dialog box for new computer name
Confirmation with option to cancel
Simple dialog box for new workgroup name
Confirmation with option to cancel
Request to "Reboot Now"
I am not solid with scripting (by any stretch), but I have mashed together more than a few via copy and paste and research before.  I have VBS scripts that do a nice job with the InputBox & Computer Name and reboot portion.  However, I keep getting stuck on the InputBox & workgroup part of this.  In my domain, I simply do this myself from my local desktop via netdom and an elevated command prompt with admin credentials.  These are not options for this.
A little about the machines -
freshly imaged using ghost
Windows 7 Professional 64bit
machines have never and will never be part of a domain
not sysprepped
default user is an admin

with no password
any error should revert or not commit any changes

The script I would like to modify or complete is below.  I like it because it is simple and clean and meets the clients requirements and I have found it to be reliable.
StrComputer = "."
Set objShell = CreateObject( "WScript.Shell" )
origname = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

NewName = InputBox("Enter new computer name: ","Computer name changer",origname)

If Vartype(NewName) = 0 Then Call endscript

Sub endscript

MsgBox "Cancelled." & vbCrLf & vbCrLf & "Computer name will remain as: " & origname, vbInformation, "Cancelled by user"
	Wscript.quit
End Sub

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
 ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
 MsgBox "About to rename computer to: " & NewName
 ErrCode = objComputer.Rename(NewName)
 If ErrCode = 0 Then
 MsgBox "Computer renamed correctly. Rebooting to finish SOE configuration.", vbInformation, "Success!"
 Else
 MsgBox "Eror changing computer name. Error code: " & ErrCode, vbInformation, "Error!"
 End If
set objShell = wscript.CreateObject("wscript.shell")
objShell.Run "shutdown.exe /R /T 5 /C ""Rebooting your computer now!"" "

Next

Open in new window

My desire is to:
insert the Workgroup name change before the reboot
have it work just like the Computer name change
Show current Workgroup name
Ask for new name
Have option for cancel
finish up with a reboot and saying that changes will be applied after reboot
UAC warning is not a dealbreaker as long as it is a simple click yes or allow

I will be calling the process to start by adding to the HKLM Runonce key in the reg and this script will reside on the C drive someplace... something like:
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\RunOnce]
"rename"="C:\\Users\\FakeUser\\Desktop\\rename.vbs"

Open in new window

But probably not the desktop in the final draft, for obvious reasons.  I intend to place a .reg file somewhere with this rononece key so this new script can be rearmed for the runonce if something goes wrong or someone makes a mistake.

I love trying to figure out scripts and programming, but I just don't have the time in this case; WAN upgrade, new backup implementation, completion of Domain upgrade...just a little too much on the plate at this time.

I would love it if someone could complete my mashup.  I know someone in this community has got the info I need!

Thanks!
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, the WMI command you're looking for is JoinDomainOrWorkgroup of the Win32_ComputerSystem class:
http://msdn.microsoft.com/en-us/library/aa392154(v=vs.85).aspx


You can use that to join the workgroup, and the rest of your code looks like it should work.

Rob.
Avatar of snafumaster

ASKER

Thanks, RobSampson.
I am familiar with that. The problem for me is not the command itself, but using it in conjunction with the clients criteria.  As well, my limited scripting familiarity and available time.
I may be asking for too much.  I was hoping someone might have a script already on hand they wouldn't mind sharing or be able to tell me, with some specificity, what I need to do to the one I have.

I feel like I'm being lazy here, but I have tried mashing together several scripts to no avail.  I've spent more time than it would have taken to teach a team, if I taught them one by one, on how to do it manually.  But, alas, client's criteria.

Here's to hoping!
Matty
I'm not at a computer so I can't provide all of the code you need, but I will be able to on Tuesday.  If you use
Set objNetwork = CreateObject("WScript.Network")
strDomain = objNetwork.UserDomain

You can get the current workgroup name.

Then copy your existing InputBox section and add in the JoinWorkgroupOrMethod call if they confirm it.

If you can add the script to the RunOnce key, it should work out.

Rob.
Thanks for the tips, Rob.  I have a little time this morning and maybe some more this eve and over the weekend, so I am going to try using your advice.  I'll update the thread if I come up with something or have a question in the interim.
I figured I should update.
I thought I was well on my way.  I had half of what I wanted and thought I just needed to modify the rest to suit my needs.  So, I decided that I would do as I used to do when I was teaching myself to make webpages.  I broke things up into functional bits and use remarks and comments to segment them.  Now, I have error code 5.

Here is the first segment in one vbs:
renameCN.vbs
'---start computername change---
StrComputer = "."
Set objShell = CreateObject( "WScript.Shell" )
origname = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

NewName = InputBox("Enter new computer name: ","Computer name changer",origname)

If Vartype(NewName) = 0 Then Call endscript

Sub endscript

MsgBox "Cancelled." & vbCrLf & vbCrLf & "Computer name will remain as: " & origname, vbInformation, "Cancelled by user"
	Wscript.quit
End Sub

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
 ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
 MsgBox "About to rename computer to: " & NewName
 ErrCode = objComputer.Rename(NewName)
 If ErrCode = 0 Then
 MsgBox "Computer renamed correctly. Rebooting to finish SOE configuration.", vbInformation, "Success!"
 Else
 MsgBox "Eror changing computer name. Error code: " & ErrCode, vbInformation, "Error!"
'---end computername change---
End If

Next

Open in new window


And here is the second part for the workgroup.  I think I am totally on the wrong track here, but the inputboxes look correct.
renameWG.vbs
'---start workgroup rename---
'strDomain = objNetwork.UserDomain

strDomain = "."
Set objshell = CreateObject("WScript.shell")
origgroup = objShell.ExpandEnvironmentStrings("%USERDOMAIN%")

NewName = InputBox("Enter new Workgroup name: ","Workgrup name changer",origgroup)

If Vartype(NewName) = 0 Then Call endscript

Sub endscript

MsgBox "Cancelled." & vbCrLf & vbCrLf & "Workgroup name will remain as: " & origgroup, vbInformation, "Cancelled by user"
	Wscript.quit
End Sub

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strDomain & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
 ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
 MsgBox "About to rename workgroup to: " & NewName
 ErrCode = objComputer.Rename(NewName)
 If ErrCode = 0 Then
 MsgBox "Workgroup renamed correctly. Rebooting to finish SOE configuration.", vbInformation, "Success!"
 Else
 MsgBox "Eror changing Workgroup name. Error code: " & ErrCode, vbInformation, "Error!"
 End If
'---end workgroup rename---

'---start reboot---
'set objShell = wscript.CreateObject("wscript.shell")
'objShell.Run "shutdown.exe /R /T 5 /C ""Rebooting your computer now!"" "

Next

Open in new window


To restate my goal:
No computer skills required
Starts on initial boot
Simple dialog box for new computer name
Confirmation with option to cancel
Simple dialog box for new workgroup name
Confirmation with option to cancel
Request to "Reboot Now"

I feel I have somehow damaged the OS.  Even my original script in the OP comes back with an error 5.  So Intend to reimage it tomorrow, given the time.  This is a side project from a high level client that was requested.  Time is at a premium for me.

Any help is appreciated!
Thanks,
Matty
Hi, I haven't tested this, but I have fixed it up for you, so I think it should work.

Regards,

Rob.

'---start computername change---
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
strComputer = objNetwork.ComputerName

strNewName = InputBox("Enter new computer name: ", "Computer name changer", strComputer)

If Vartype(NewName) = 0 Or strNewName = strComputer Then
	MsgBox "Cancelled." & vbCrLf & vbCrLf & "Computer name will remain as: " & strComputer, vbInformation, "Cancelled by user"
Else
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	Set colComputers = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
	For Each objComputer in colComputers
		MsgBox "About to rename computer to: " & NewName
		ErrCode = objComputer.Rename(NewName)
		If ErrCode = 0 Then
			MsgBox "Computer renamed correctly. Rebooting to finish SOE configuration.", vbInformation, "Success!"
		Else
			MsgBox "Eror changing computer name. Error code: " & ErrCode, vbInformation, "Error!"
		End If
	Next
End If
'---end computername change---


'---start workgroup rename---
Set objNetwork = CreateObject("WScript.Network")
strDomain = objNetwork.UserDomain
strComputer = objNetwork.ComputerName
Set objShell = CreateObject("WScript.shell")

strNewWorkgroup = InputBox("Enter new Workgroup name: ","Workgrup name changer", strDomain)

If Vartype(strNewWorkgroup) = 0 Or strNewWorkgroup = strDomain Then
	MsgBox "Cancelled." & vbCrLf & vbCrLf & "Workgroup name will remain as: " & strDomain, vbInformation, "Cancelled by user"
Else
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
MsgBox "About to rename workgroup to: " & NewName
ErrCode = objComputer.JoinDomainOrWorkGroup(strNewWorkgroup)
If ErrCode = 0 Then
	MsgBox "Workgroup renamed correctly. Rebooting to finish SOE configuration.", vbInformation, "Success!"
Else
	MsgBox "Eror changing Workgroup name. Error code: " & ErrCode, vbInformation, "Error!"
End If
'---end workgroup rename---

'---start reboot---
'set objShell = wscript.CreateObject("wscript.shell")
'objShell.Run "shutdown.exe /R /T 5 /C ""Rebooting your computer now!"" "

Open in new window

First, let me say Thank You for your assistance; it is very much appreciated!
Second, my edits are not critique, just me trying to be clear on the steps I've taken.

Looks like some serious progress.  I am still having some doubt of this machines integrity.  With respect to error code 5, but let me first update after testing.

You'll no doubt notice that I made a couple of changes.  I think you had a couple of typos.
Where I had it "NewName" in the computer name change starting at the inputbox, you modified it to "strNewName".  There were a couple of places where it still reference "NewName".  When testing, after putting in the new computer name into the box, hitting OK resulted in a cancel.  Changing them to your strNewName seemed to correct this.
In the workgroup name change section, there was a similar issue.  You used "strNewWorkgroup" to replace my use of "NewName", but there was one reference to "NewName".
When testing after putting in the new workgroup name and hitting OK, it resulted in echo of Changing workgroup name to <blank>. (not literally...it was just empty of course)  Changing those appears to have corrected that.
In VbsEdit, on the last line, it stated that it expected a an "End".  I added the end and then it said it expected a an "If".  I added that and now it runs through and looks great...with the exception of Error Code 5 after hitting OK after each phase (Changing computer name to: and Changing Workgroup name to:)
The edited code is below:
'---start computername change---
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
strComputer = objNetwork.ComputerName

strNewName = InputBox("Enter new computer name: ", "Computer name changer", strComputer)

If Vartype(strNewName) = 0 Or strNewName = strComputer Then
	MsgBox "Cancelled." & vbCrLf & vbCrLf & "Computer name will remain as: " & strComputer, vbInformation, "Cancelled by user"
Else
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	Set colComputers = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
	For Each objComputer in colComputers
		MsgBox "About to rename computer to: " & strNewName
		ErrCode = objComputer.Rename(strNewName)
		If ErrCode = 0 Then
			MsgBox "Computer renamed correctly. Rebooting to finish SOE configuration.", vbInformation, "Success!"
		Else
			MsgBox "Eror changing computer name. Error code: " & ErrCode, vbInformation, "Error!"
		End If
	Next
End If
'---end computername change---


'---start workgroup rename---
Set objNetwork = CreateObject("WScript.Network")
strDomain = objNetwork.UserDomain
strComputer = objNetwork.ComputerName
Set objShell = CreateObject("WScript.shell")

strNewWorkgroup = InputBox("Enter new Workgroup name: ","Workgrup name changer", strDomain)

If Vartype(strNewWorkgroup) = 0 Or strNewWorkgroup = strDomain Then
	MsgBox "Cancelled." & vbCrLf & vbCrLf & "Workgroup name will remain as: " & strDomain, vbInformation, "Cancelled by user"
Else
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
MsgBox "About to rename workgroup to: " & strNewWorkgroup
ErrCode = objComputer.JoinDomainOrWorkGroup(strNewWorkgroup)
If ErrCode = 0 Then
	MsgBox "Workgroup renamed correctly. Rebooting to finish SOE configuration.", vbInformation, "Success!"
Else
	MsgBox "Eror changing Workgroup name. Error code: " & ErrCode, vbInformation, "Error!"
'---end workgroup rename---

End If
'---start reboot---
'set objShell = wscript.CreateObject("wscript.shell")
'objShell.Run "shutdown.exe /R /T 5 /C ""Rebooting your computer now!"" "
End If

Open in new window

Yeah sorry, I tend to have typos when I start renaming variables for clarity.  From memory, return code 5 means access is denied.  Can you run an elevated command prompt, and run
Cscript c:\scripts\test.vbs

And see if that works?

Rob.
That's it, Rob!
Running the script as posted above from an elevated command prompt did the trick.  

Now to look into calling this script automatically from an elevated command prompt. This is more of an area of comfort for me.

Thanks again for all of your help, Rob.
After correcting the couple of typos and running the script from an elevated command prompt, the script work very well.
No problem. Glad to help. Thanks for the grade.

Rob.
I did just notice one little flaw.  When it reports the current Workgroup name in the inputbox, it shows the current computer name.  In a quick look, I cannot see why.  Hopefully it is something very simple to edit.

As a sidenote, I did add something to the script so that it runs elevated so that you don't have to call it using cscript from an elevated command prompt.

See here:
'---Start of UAC workaround code

If WScript.Arguments.length =0 Then
  Set objShell = CreateObject("Shell.Application")

  objShell.ShellExecute "wscript.exe", Chr(34) & _
  WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else

'---End UAC part one
'---start computername change---
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
strComputer = objNetwork.ComputerName

strNewName = InputBox("Enter new computer name: ", "Computer name changer", strComputer)

If Vartype(strNewName) = 0 Or strNewName = strComputer Then
	MsgBox "Cancelled." & vbCrLf & vbCrLf & "Computer name will remain as: " & strComputer, vbInformation, "Cancelled by user"
Else
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	Set colComputers = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
	For Each objComputer in colComputers
		MsgBox "About to rename computer to: " & strNewName
		ErrCode = objComputer.Rename(strNewName)
		If ErrCode = 0 Then
			MsgBox "Computer renamed correctly and will change after reboot.", vbInformation, "Success!"
		Else
			MsgBox "Eror changing computer name. Error code: " & ErrCode, vbInformation, "Error!"
		End If
	Next
End If
'---end computername change---

'---start workgroup rename---
Set objNetwork = CreateObject("WScript.Network")
strDomain = objNetwork.UserDomain
strComputer = objNetwork.ComputerName
Set objShell = CreateObject("WScript.shell")

strNewWorkgroup = InputBox("Enter new Workgroup name: ","Workgrup name changer", strDomain)

If Vartype(strNewWorkgroup) = 0 Or strNewWorkgroup = strDomain Then
	MsgBox "Cancelled." & vbCrLf & vbCrLf & "Workgroup name will remain as: " & strDomain, vbInformation, "Cancelled by user"
Else
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
MsgBox "About to rename workgroup to: " & strNewWorkgroup
ErrCode = objComputer.JoinDomainOrWorkGroup(strNewWorkgroup)
If ErrCode = 0 Then
	MsgBox "Workgroup renamed correctly and will change after reboot.", vbInformation, "Success!"
Else
	MsgBox "Eror changing Workgroup name. Error code: " & ErrCode, vbInformation, "Error!"
'---end workgroup rename---

End If
'---start reboot---
set objShell = wscript.CreateObject("wscript.shell")
objShell.Run "shutdown.exe /R /T 5 /C ""Rebooting your computer now!"" "
End If

'---end workgroup rename---
'---End of UAC workaround code

End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Outstanding; really!
Works perfectly.  You've helped a bunch and I've learned a bunch as well.

Thanks again,
Matty
Perfect. I agree.
Thank you!