Jump to content

Recommended Posts

Posted

Here is a script I put together for some remote computer renaming that I need to do - don't bash the coding skills I'm a Systems Engineer by trade :D

 

'#############
'# Get Host  #
'#############
On error resume next
strComputer = WScript.arguments.item(0)

if strComputer = "" then
strComputer  = InputBox( "Name of computer to rename" )
if strComputer = "" then
wscript.echo "No computer name suppled"	
wscript.quit
end if
end if

'##########
'# Ping   #
'##########

   Dim colPingResults, objPingResult, strQuery

   ' Define the WMI query
   strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & strcomputer & "'"

   ' Run the WMI query
   Set colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )

   For Each objPingResult In colPingResults
       If Not IsObject( objPingResult ) Then
           Ping = False
       ElseIf objPingResult.StatusCode = 0 Then
           Ping = True
       Else	
    Wscript.Echo "Computer COULD NOT be contacted - Exiting"
    wscript.quit
           Ping = False
       End If
   Next

   Set colPingResults = Nothing

'##########
'# Get SN #
'##########
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery _
   ("Select * from Win32_BIOS")
For each objBIOS in colBIOS
strSerialNumber = trim(objBIOS.SerialNumber)
Next

'##############
'# Get Info   #
'##############
strSerialPrompt = "Hint: SN is " & strSerialNumber

if strSerialNumber = "N/A" then
strSerialPrompt = "Serial Number Not Available"
strSerialNumber = ""
end if

if strSerialNumber = "" then
strSerialPrompt = "Serial Number Not Available"
strSerialNumber = ""
end if

strNewComputer  = InputBox( "Enter new computer name - " & strSerialPrompt, "New Computer", "???-??-" & strSerialNumber) 
if strNewComputer = "" then
wscript.echo "No new computername suppled"	
wscript.quit
end if
strDomainUser   = InputBox( "Enter username in this format: domain\username" )   
if strDomainUser = "" then
wscript.echo "No Domain User suppled"	
wscript.quit
end if
strDomainPasswd = InputBox( "Enter account password" )
if strDomainPasswd = "" then
wscript.echo "No Domain Password suppled"	
wscript.quit
end if

'#############
'# Connect   #
'#############
set objWMILocator = CreateObject("WbemScripting.SWbemLocator")
objWMILocator.Security_.AuthenticationLevel = 6
set objWMIComputer = objWMILocator.ConnectServer(strComputer,  _
          		                         "root\cimv2", _
                                                 strDomainUser, _
                                                 strDomainPasswd)
set objWMIComputerSystem = objWMIComputer.Get( _
                              "Win32_ComputerSystem.Name='" & _
                              strComputer & "'")
'##########
'# Rename #
'##########

rc = objWMIComputerSystem.Rename(strNewComputer, _
                                strDomainPasswd, _
                                strDomainUser)
if rc <> 0 then
   WScript.Echo "Rename failed with error: " & rc
else
   WScript.Echo "Successfully renamed " & strComputer & " to " & _
                strNewComputer
end if

'##########
'# Reboot?#
'##########
intAnswer = _
   Msgbox("Do you want to reboot the remote computer?", _
       vbYesNo, "Reboot")

If intAnswer = vbYes Then
   Set objOSSet = GetObject("winmgmts:{(RemoteShutdown)}//" & strComputer & "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")

   For each objOS in objOSSet
objOS.Reboot()
   Next

Else
'No
End If

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...