Avatar of TheFlyingCorpse
TheFlyingCorpseFlag for Norway asked on

How can I change computername using a script?

Hello,

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"
Const WSNAME_PARAMETERS = "/NOREBOOT"
 
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)
    WScript.Quit(1)
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"
    WScript.Quit(1)
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
    GetWMIStuff=False
    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
                strResult=propWMI.Value
                GetWMIStuff=True
                Exit Function
            End If
        Next
    Next
End Function
 
' --------------------------------------------------------------
 
Function TidySerialNumber(strSerialNumber)
    Dim strModel
    On Error Resume Next   ' Turn off error checking
    TidySerialNumber=strSerialNumber
    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
TheFlyingCorpse

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
TheFlyingCorpse

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Your help has saved me hundreds of hours of internet surfing.
fblack61