Bulk ComputerName Changer

This tool is great for network administrators who may ghost their harddrives with duplicate versions of windows starting off with the same computername. The tool can use a database,random number or even harddrive serial number. You must run it as an administrator in order to work. It checks to see if it can lookup it’s computername. So if you use the number range make sure to reboot the computer before going to the next computer.

Here are some examples

compname.exe name=server?x /d

The command above takes the harddrive serial number in hexadecimal and puts it into the computer name

compname.exe name=server?d start=1000 end=1005 file=example.txt /w

The command above sets the computername with its number variable(?d) to a number between 1000 and 1005 and puts it in decimal and stores it into example.txt file.

compname.exe name=justin-?d end=128 /r

Sets the computer name wirh a random number between 0 to 127

compname.exe name=mailserver

Sets the computer name to “mailserver”

System Requirements:

Windows XP or higher maybe 2000 or NT4 it will not work on non-NT Type windows such as(windows 95, 98 or Me)

unit compname1;

interface
uses windows,sysutils,classes,winsock;
function delphianMain(commandline:tstringlist):dword;

implementation
var config:tstringlist;
storetodatabase:boolean=false;
hdd:dword;
fn:string;

function NTSetPrivilege(sPrivilege: string; bEnabled: Boolean): Boolean;
var
  hToken: THandle;
  TokenPriv: TOKEN_PRIVILEGES;
  PrevTokenPriv: TOKEN_PRIVILEGES;
  ReturnLength: Cardinal;
begin
  Result := True;
  // Only for Windows NT/2000/XP and later.
  if not (Win32Platform = VER_PLATFORM_WIN32_NT) then Exit;
  Result := False;

  // obtain the processes token
  if OpenProcessToken(GetCurrentProcess(),
    TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
  begin
    try
      // Get the locally unique identifier (LUID) .
      if LookupPrivilegeValue(nil, PChar(sPrivilege),
        TokenPriv.Privileges[0].Luid) then
      begin
        TokenPriv.PrivilegeCount := 1; // one privilege to set

        case bEnabled of
          True: TokenPriv.Privileges[0].Attributes  := SE_PRIVILEGE_ENABLED;
          False: TokenPriv.Privileges[0].Attributes := 0;
        end;

        ReturnLength := 0; // replaces a var parameter
        PrevTokenPriv := TokenPriv;

        // enable or disable the privilege

        AdjustTokenPrivileges(hToken, False, TokenPriv, SizeOf(PrevTokenPriv),
          PrevTokenPriv, ReturnLength);
      end;
    finally
      CloseHandle(hToken);
    end;
  result:=getlasterror=error_success;
  end;
end;

procedure RebootSystem(compn:pchar);
begin
if not ntsetprivilege('SeShutdownPrivilege',true)then writeln('PrivilegeErr:',
syserrormessage(getlasterror));
if not InitiateSystemShutdown(nil,pchar('ComputerName was changed to '+strpas(
compn)),10,true,true)then messagebox(0,pchar('The computer is now called '+
strpas(compn)+#13#10'But failed to reboot'#13#10+syserrormessage(getlasterror)),
'compname',mb_iconinformation);
ntsetprivilege('SeShutdownPrivilege',false);
end;

procedure AddComputerConfig(compn:pchar);
begin
if (not storetodatabase) then exit;
try
config.Values[IntToHex(hdd,8)]:=strpas(compn);
config.SaveToFile(fn);
except on e:exception do writeln('Failed to store name into database: ',
e.message);end;
end;

function delphianMain(commandline:tstringlist):dword;
var data:wsadata;
i,start,endn:integer;
fname:string;
dummy1,dummy2:dword;
windir:array[0..max_path]of char;
compn:array[0..max_computername_length]of char;
label rndname,spotfound,skipover;
{$RESOURCE COMPNAME32.RES}
begin
result:=0;
hdd:=0;
storetodatabase:=(Commandline.IndexOf('/w')>-1);
if storetodatabase and (commandline.Values['name']='')then begin
result:=DWord(-2);
writeln('The /w command switch needs a name= parameter');
exit;
end;
if storetodatabase and (commandline.Values['file']='')then begin
result:=dword(-3);
writeln('Can'#39't store the name into a file without a filename');
exit;
end;
getwindowsdirectory(windir,max_path+1);
getvolumeinformation(pchar(windir[0]+':\'),nil,0,@hdd,dummy1,dummy2,nil,0);
if (commandline.values['name']='')and(commandline.values['file']='')then begin
if commandline.Count>0then
writeln(
'You forgot to define name= or file= in the command, please choose one and try again');
writeln('Usage: ',extractfilename(paramstr(0)),' [start=] [end=] [name=] [file=] [/w] [/r] [/re] [/d]');
writeln('start  Starting number, 0 is default');
writeln('end    Ending number default is start+255');
writeln('name   Computernaming, this or file must be specified');
writeln('/w     Writes to the text file the computer name chosen plus the hdd serial number(if not found in the database) the filename is from file= parameter.');
writeln('/re    Reboot the computer on a successful change.');
writeln('/r     Generates a random number in the name. Only [end=] is the optional parameter for this switch.');
writeln('/d     Uses the main harddrive serialnumber');
writeln('file   Gets computername from a text file and uses harddrive serial number to lookup the name');
writeln('Number Format specifers:');
writeln('?d     Decimal number');
writeln('?x     Hexadecimal number');
writeln('example: ',extractfilename(paramstr(0)),
' start=100 end=199 name=client?d');
writeln('This computer'#39's harddrive serial number is ',inttohex(hdd,8));
writeln('Max. length:',max_computername_length,' characters.');
writeln('Press enter to return...');
readln;
exit;
end;
randomize;
if commandline.Values['file']<>''then begin
config:=tstringlist.Create;
if fileexists(commandline.values['file']) then
config.LoadFromFile(commandline.values['file']);
fn:=commandline.Values['file'];
if(config.Values[inttohex(hdd,8)]='')and(commandline.IndexOf('/w')>-1)then goto skipover;
if setcomputername(Pchar(config.values[inttohex(hdd,8)]))then writeln('My name is ',
config.values[inttohex(hdd,0)])else begin result:=getlasterror;writeln(
syserrormessage(getlasterror));exit;end;
end;
skipover:
wsastartup($101,data);
fname:=stringreplace(commandline.values['name'],'?','%',[rfreplaceall]);
// Use ? instead of % in the name to make batch files easier
setlasterror(0);
if commandline.IndexOf('/d')>-1then begin
if setcomputername(strfmt(compn,pchar(fname),[hdd]))then writeln('My name is ',
compn)else begin
result:=getlasterror;
writeln(syserrormessage(getlasterror));
wsacleanup;
exit;
end;
AddComputerConfig(compn);
if commandline.indexof('/re')>-1then rebootsystem(compn);
wsacleanup;
exit;
end;
if commandline.IndexOf('/r')>-1Then begin
rndname:
if gethostbyname(strfmt(compn,pchar(fname),[random(strtointdef(commandline.values[
'end'],maxint))]))<>nil then goto rndname;
if setcomputername(compn) then writeln('My name is ',compn) else
begin
writeln(syserrormessage(getlasterror));
result:=getlasterror;
wsacleanup;exit;
end;
addcomputerconfig(compn);
if commandline.indexof('/re')>-1then rebootsystem(compn);
wsacleanup;
exit;
end;
start:=strtointdef(commandline.values['start'],0);
endn:=strtointdef(commandline.values['end'],0);
if endn-start<=0then endn:=start+255;
for i:=start to endn do if gethostbyname(strfmt(compn,pchar(fname),[i]))=nil then
goto spotfound;
writeln('All spots taken.');
wsacleanup;
result:=dword(-1);
exit;
spotfound:if setcomputername(strfmt(compn,pchar(fname),[i]))then
writeln('My name is ',compn)else begin result:=getlasterror;writeln(syserrormessage(
getlasterror));
addcomputerconfig(compn);
if commandline.indexof('/re')>-1then
rebootsystem(compn);
wsacleanup;
end;
end;
end.

Published by Justin Roeder

I am an electronics engineer and computer programmer that has autism. I learned by myself

Leave a comment

Your email address will not be published. Required fields are marked *