views:

214

answers:

2

I'm trying to make a script to rename PC's to their serial number. I'm not great with VB, but I've been able to put together enough code to READ the serial number, but I'm not sure where to WRITE it.

Here's what I currently have:

    strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")


Set colBIOS = objWMIService.ExecQuery("Select * from Win32_BIOS")

For each objBIOS in colBIOS
  WScript.Echo "SERIAL=" & objBIOS.SerialNumber
Next

Thanks in advance for any help!

A: 

I doubt you can change the computer name through WMI. There is a Windows API call here.

Dave Swersky
A: 

You can change the computer name editing the Windows registry. use this code with extreme caution.

This code is for educational purposes only, I recommend that you use the SetComputerNameEx function and not a script to do these tasks

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colBIOS = objWMIService.ExecQuery("Select * from Win32_BIOS")
For each objBIOS in colBIOS
StrNewPCName=objBIOS.SerialNumber
Next
Set MyShell= CreateObject ("WSCript.shell")
StrCurrentControlSet = "HKLM\SYSTEM\CurrentControlSet\"
StrTcpipParams = StrCurrentControlSet & "services\Tcpip\Parameters\"
StrComputerName = StrCurrentControlSet & "Control\ComputerName\"
With MyShell
.RegDelete StrTcpipParams & "Hostname"
.RegDelete StrTcpipParams & "NV Hostname"
.RegWrite StrComputerName & "ComputerName\ComputerName", StrNewPCName
.RegWrite StrComputerName & "ActiveComputerName\ComputerName", StrNewPCName
.RegWrite StrTcpipParams & "Hostname", StrNewPCName
.RegWrite StrTcpipParams & "NV Hostname", StrNewPCName
End With
RRUZ
Thanks for the answer RRUZ, you've suggested that I use SetComputerNameEx. I'll investigate that option, but I'm not a programmer, I'm a sysadmin.
SomeDudePPF