Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBscript file SetComputerName

Status
Not open for further replies.

Ouch

Programmer
Jul 17, 2001
159
GB
i am trying to write a simple VBscript file to change the computer name.

i have machines that have been 'ghosted' and want to change the computer name afterwards.

i do not have VB Developer software but i was told it is possible to write it in a text script and call it .vbs and it would run?

i have tried normal functions but they dont work..

script:...


Private Declare Function SetComputerName Lib "kernel32" Alias "SetComputerNameA" (ByVal lpComputerName As String) As Long



SetComputerName "Karthik"
End


can anyone help?
 
You can't use api calls in vbscript, see thread711-411427
I'm not an expert when it comes to scripting, but I'm afraid you can't do such drastic things using only vbscript.

Remedy
 
OH...Yes you can! i did it...

here is the final script...

it gets a unique identifier from the laptop.
then it gets the old computername
then it checks which machine it is running on and checks if the computername is correct for the ID, if it is, it deletes itself(the scritp) if not it changes the computername, reboots and then deletes itself.


'as compiled using scripts from the microsoft site and micosoft's 'scriptomatic' from the .net server resource kit

PS the laptops are XP i havent tried it on anything older

'**************get serial

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystemProduct",,48)
For Each objItem in colItems

' Wscript.Echo "IdentifyingNumber: " & objItem.IdentifyingNumber

strID=objItem.IdentifyingNumber
Next

'**************get old computer name
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems

' Wscript.Echo "Name: " & objItem.Name
strCompName=objItem.Name
Next


'Wscript.Echo "Serial: " & strID

'**************check computer name and serial

'Wscript.Echo "OldCompname: " & strCompName

If strID = "82640363G" then strNewName="CLIENT01"
'If strID = "2" then strNewName="CLIENT02"
'If strID = "82640399G" then strNewName="CLIENT03"
'If strID = "82640360G" then strNewName="CLIENT04"
'If strID = "2" then strNewName="CLIENT05"
If strID = "82640345G" then strNewName="CLIENT06"
'If strID = "2" then strNewName="CLIENT07"
If strID = "82640419G" then strNewName="CLIENT08"

'Wscript.Echo "Serial: " & strID
'Wscript.Echo "NewCompName: " & strNewName

If strCompName = strNewName then strNewName=""



'**************change computer name
strComputer = "client"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
if strNewName="" then
'do nothing
else
err = ObjComputer.Rename(strNewName)
Wscript.Echo strNewName

'**************shut down and reeboot

Set colOperatingSystems = GetObject("winmgmts:{(Shutdown)}").ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.reboot()
Next
end if
Next

'**************delete this file from startup now its finished with

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("C:\Documents and Settings\User\Start Menu\Programs\Startup\chgCompName.vbs")
 
Yep, but this isn't "using Windows APIs." It uses the "Scripting API for WMI" ActiveX Control.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top