?
Solved

Rename and Reboot Machine

Posted on 2009-07-06
2
Medium Priority
?
651 Views
Last Modified: 2013-11-26
Im trying to write this script that will rename and reboot the machine. The first portion shown runs fine, the dialog box comes up and I enter in my new pc name.  The second part is the vb command to reboot the machine, it actually calls a batch file. The script runs the first portion and then ends.  However if I take the second half and create a separate vb script it runs fine.

I have two questions:

1. How can I get the entire script to run? Am I missing something?
2. Is there any way I can create a list box rather than an Input Box and prepopulate the listbox with a list of names?
Name = InputBox("Enter the new computer name below and click OK to continue","Rename: Step 1")
 
Password = "password"
Username = "admin"
 
Set objWMIService = GetObject("Winmgmts:root\cimv2")
 
' Call always gets only one Win32_ComputerSystem object.
For Each objComputer in _
   objWMIService.InstancesOf("Win32_ComputerSystem")
 
       Return = objComputer.rename(Name,Password,User)
       If Return <> 0 Then
          WScript.Echo "Rename failed. Error = " & Err.Number
       Else
          WScript.Echo "Rename succeeded." & " Reboot for new name to go into effect"
       
        Set OpSysSet = GetObject("winmgmts:{impersonationLevel=impersonate,(RemoteShutdown)}//" & mname).ExecQuery("select * from Win32_OperatingSystem where Primary=true")
        for each OpSys in OpSysSet
            OpSys.Reboot()
next
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("%comspec% /K C:\DemoUtilities\Installs\Windows\Rename_machine\shutdown.bat"), 1, True
end if
next

Open in new window

0
Comment
Question by:dlnewman70
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Expert Comment

by:nailgg
ID: 24791812
Hi dlnewman70;

I debugged your code in vb6, and found some little mistakes.

For example the dimension you defined as "Return" is not a suitable word to being a dimension name. Change it as "ReturnX" for example.

There is a property named as "Name" too, you must change it too.

I hope these can help you.
0
 

Accepted Solution

by:
dlnewman70 earned 0 total points
ID: 24794827
Found solution

Dim Text, Title
Dim WshNetwork         ' Object variable
Text = "Networking information" & vbCrLf & vbCrLf
Title = "Rename Hostname?"
 
' Create a new WshNetwork object to access network properties.
Set WshNetwork = WScript.CreateObject("WScript.Network")
 
Text = Text & "Computer name : " & WshNetwork.ComputerName & vbCrLf
Text = Text & "Domain : " & WshNetwork.UserDomain & vbCrLf
Text = Text & "User name : " & WshNetwork.UserName & vbCrLf & vbCrLf
Text = Text & "Do you want to rename this computer?"
 
If MsgBox(Text, vbYesNo + vbQuestion, Title) = vbyes Then
        Dim strComputerName, objWMIService, objComputer, returnCode
 
        strComputerName = InputBox("Enter a new machine name for this computer:", "Rename Computer")
 
        If strComputerName <> "" Then 
 
                Set objWMIService = GetObject("Winmgmts:root\cimv2") 
 
                ' Call always gets only one Win32_ComputerSystem object. 
                For Each objComputer in objWMIService.InstancesOf("Win32_ComputerSystem")
                        ReturnCode = objComputer.Rename(strComputerName) 
                Next
                If ReturnCode <> 0 Then
                        MsgBox "There was a problem changing the computer name.  System error code : " & ReturnCode
                Else
                        If MsgBox("Changes made.  Do you wish to restart the computer?", vbYesNo + vbQuestion, Title) = vbyes Then
                                                                                                                                'Set objShell = WScript.CreateObject("WScript.Shell")
                                'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
                                                                                                                                WScript.Echo "Rebooting..."
                                                                                                                                set objWSHShell = WScript.CreateObject("WScript.Shell")
                                                                                                                                objWSHShell.Run "shutdown.exe /r"
                        Else 
                                MsgBox "You must restart computer for changes to take effect"
                        End If 
                End If 
        End If 
End If

Open in new window

0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

777 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