Avatar of TheFlyingCorpse
TheFlyingCorpseFlag for Norway

asked on 

How can I change computername using a script?


I am trying to automate a install for Windows Vista, however I am stuck when it comes to changing the name of the computer to the standards we use.

I've used a VB script for this on XP, but when I move this to Vista, I have to change some more strings for it to function properly. It reads the name correct, but after the boot the computername seems to be overwritten.

I've searched the net for solutions to changing it this way and that, but I am unable todo so.

I've also discovered that WMI is disabled during Audit mode, so this has to be done either during the early stages of setup or OOBE. The script I have been trying to use is attached.
' Example VB Script for passing serial number from BIOS to WSName 
' Edit Const's as required 
' Note that some BIOS's may be require the serial number to be trimmed to
' fit within the allows 15 characters, my Toshiba Tecra 8100 for example  
' returns : 70013268,PT810A-12C52,PMAT2A12C522/S3A0305D001
' The sticker on the back just says : 70013268J, as I can determine the 
' manufacturer and model I can trim this down easily enough
' Some BIOS offer the ability to store asset information, I don't have access 
' to one of these for testing but I assume the process will be very similar
' WMI support was introduced with Windows 2000 (?check this) so you'll need to
' use another mechanism to retrieve the serial number on these machines
Option Explicit
Const PATH_TO_WSNAME    = "%systemroot\setup\BKB\WSNAME.EXE"
Dim WSHShell1
Set WSHShell1 = WScript.CreateObject("WScript.Shell")
Dim strSerialNumber, strTEMP, strCommandLine, fso, WshShell
set fso = CreateObject("Scripting.FileSystemObject")
set WshShell = WScript.CreateObject("WScript.Shell")
'Check WSName exists
if not fso.FileExists(PATH_TO_WSNAME) then
    WScript.Echo "ERROR, Could not find WSNAME in " & fso.GetParentFolderName(PATH_TO_WSNAME)
end if
' --- Get the serial number, abort script on error
if Not GetWMIStuff("Win32_BIOS","SerialNumber",strSerialNumber) then
    WScript.Echo "ERROR, Could not retrieve serial number"
end if
' --- If we got this far then we have the serial number, if you want to see it
' --- uncomment the next line
' WScript.Echo "The raw serial number is " & strSerialNumber
' --- Tidy up the serial number, if required
strSerialNumber = TidySerialNumber(strSerialNumber)
' --- Uncomment the next line if you want to see it the result
' WScript.Echo "The tidied serial number is " & strSerialNumber
' --- Add leading Alpha characters to avoid DNS confusion
strSerialNumber="VWS-" & strSerialNumber 
' strSerialNumber=strSerialNumber
' WScript.Echo "The tidied serial number is " & strSerialNumber
WSHShell1.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", strSerialNumber
WSHShell1.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability\LastComputerName", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName\ComputerName", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\ControlSet001\Control\ComputerName\ComputerName", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\ControlSet001\Services\Tcpip\Parameters\Hostname", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\ControlSet001\Services\Tcpip\Parameters\NV Hostname", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\ComputerName", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\CurrentControlSet\ControlSet\Control\ComputerName\ComputerName", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Hostname", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", strSerialNumber
' --- Build Command Line, to check out the command line uncomment
strCommandLine = PATH_TO_WSNAME & " /N:" & strSerialNumber & " " & WSNAME_PARAMETERS
' WScript.Echo "About to run the following command:" & vbCRLF & vbCRLF & strCommandLine
' --- Now call WSName with the serial number
Call WshShell.Run(strCommandLine,1,FALSE)
' --- Game Over!
' ---------------------- Helper Functions ----------------------
Function GetWMIStuff(strInstance,strProperty,strResult)
    Dim colWMIStuff, objWMI, propWMI
    Set colWMIStuff= GetObject("winmgmts:").InstancesOf(strInstance)
    For Each objWMI In colWMIStuff
        For each propWMI in objWMI.Properties_
            If (Not IsNull(propWMI.Value) AND UCase(propWMI.Name) = UCase(strProperty)) Then
                Exit Function
            End If
End Function
' --------------------------------------------------------------
Function TidySerialNumber(strSerialNumber)
    Dim strModel
    On Error Resume Next   ' Turn off error checking
    Call GetWMIStuff("Win32_BIOS","Description",strModel)
    if Instr(UCase(strModel),"TECRA8100") then   ' This will a Tecra8100. I know how to tidy up. 
        TidySerialNumber=Left(strSerialNumber,Instr(strSerialNumber,",")-1) ' Trim at the first comma
    end if  
    On Error Goto 0        ' Turn error checking back on 
End Function

Open in new window

Windows OSWindows Vista

Avatar of undefined
Last Comment
Avatar of TheFlyingCorpse
Flag of Norway image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Windows OS
Windows OS

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo