Jump to content

Need help renaming computers


Recommended Posts

I am trying toi write a vb script that will check the machines current subnet and serial number, once it has discovered its subnet, check for a 3 letter code assigned to that subnet. Then take that 3 letter code and prefix it to the serial number and rename the computer with that name. I really suck at VB and can't figure it out. I found a script that uses wsname to rename a computer, but dont know how to refence a text file with the subnet litings. here is the wsname code:

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 site and serial number, if you want to see it
' --- uncomment the next line
WScript.Echo "The raw serial number is" & strSerialNumber
' --- Add leading Alpha characters to avoid DNS confusion
strSerialNumber=  "site" & 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 Uelse(propWMI.Name) = Uelse(strProperty)) Then
               strResult=propWMI.Value
               GetWMIStuff=True
               Exit Function
                     End If

Tried to add this into the mix,but no luck:

subnet
subnett = subnet

If  subnett = "10.2.144" then site = "AMS"

       elseif subnett = "10.2.20" then site = "ATL"
end if

Link to comment
Share on other sites


Hi,

Not sure about the script you posted, dont think it'll run.

but you might like to try this script that i wrote. Itz a bit messy so any pointers greatfully received :)

Please bare in mind that if you have multiple instances of tcp running ie multiple nics virtual nics etc it will run multiple times on that computer!

Also i dont have anyway to check if this script is getting valid info for the serial number..... it should but i cant check

'Local Computer

strComputer = "."

'Change next two lines for relevant subnets

AMSsubnet = "192.168.0"

ATLsubnet = "192.168.174"

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

Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

Dim colSettingsComp : Set colSettings = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")

Dim colSettingsBios : Set colSettingsBios = objWMIService.ExecQuery("Select * from Win32_BIOS")

Dim objComputer, strSerial

'Uncomment next three lines if you want Computer Model

'For Each objComputer in colSettings

' strModel = objComputer.Model

'Next

For Each objComputer in colSettingsBios

strSerial = objComputer.SerialNumber

Next

'next line to display Computer serial number if found - test purposes only

wscript.echo strSerial

'test which subnet computer IP matches then append AMS/ATL to beginning of serial number

For Each IPConfig in IPConfigSet

If Not IsNull(IPConfig.IPAddress) Then

'Change the number 9 on line below to match the total number of characters in your subnet - dont include the zero example is 255.255.255.0

if AMSsubnet = LEFT(IPConfig.IPAddress(i),9) then

'If IP matches AMS subnet SET computer name to AMS + strSerial - replace "1234" with strSerial

CompName = "AMS" + "1234"

'next line to display Computer serial number if found - test purposes only

wscript.echo IPConfig.IPAddress(i), CompName

'Change the number 11 on line below to match the total number of characters in your subnet - dont include the zero example is 255.255.255.0

Elseif ATLsubnet = LEFT(IPConfig.IPAddress(i),11) then

'If IP matches AMS subnet SET computer name to ATL + strSerial - replace "1234" with strSerial

CompName = "ATL" + "1234"

'next line to display Computer serial number if found - test purposes only

wscript.echo IPConfig.IPAddress(i), CompName

end if

End If

Next

'Change computer name to new one - change wont ask for reboot

WSHShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", CompName

WSHShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", CompName

just cut 'n' paste in to a text doc then rename to "wotever" with a .vbs extenstion.

Hope this helps

:hello:

Link to comment
Share on other sites

My script doesn't run for crap. I tried combining pieces of scripts but did not work. I am a more of a hardware guy, not scripting. Just started playin a few days ago. I will try your script and let you know what happens. Thanks for your help!

Mike

Link to comment
Share on other sites

true valid subnet's must have 4 octets but if i understand mls15000 he is using a 255.255.255.0 mask which means the IP's he listed are the network IP's without the zero. As he is only interested in the network portion of the IP info we dont need to bother with the last octet

;)

Correct me if im wrong

Link to comment
Share on other sites

Yes, all my subnet masks are 255.255.255.0

I tested with a virtual machine and after a little tweaking, it seems like it will do the trick.

Was getting error on these lines

WSHShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", CompName
WSHShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", CompName

I added this to the beginning of the script

Set shell = CreateObject("WScript.Shell")

Then changed the trouble lines to

Shell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", CompName
Shell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", CompName

Scrit now runs fine and sets the computername, (in a virtual world). Now have to add about 30 different subnets to the code and test some of my sites!

Do I need to adjust this for my different subnets if they are all the same subnet mask?

'Change the number 9 on line below to match the total number of characters in your subnet - dont include the zero example is 255.255.255.0 
if AMSsubnet = LEFT(IPConfig.IPAddress(i),9) then

I really appreciate your help. Was originally going to use a script I tweaked that queried the MAC address and had it reference a txt file for the correct computer name.

Edited by mls15000
Link to comment
Share on other sites

Glad it appears to do the trick!! My appologies the code i posted was a mod of some code i wrote to do something similar and i forgot to add

set WshShell = WScript.CreateObject("WScript.Shell")

But your line is just as good :thumbup

If these puters are in an AD enviroment it would be better to change their names via a logon script but yes if the subnet is the same you can create any number of

lines to check for (might get even messier)

Just add a line at the top for network in question

AMSsubnet = "192.168.0"

DMZsubnet = "10.1.1"

and add the follow for each network

Elseif DMZsubnet = LEFT(IPConfig.IPAddress(i),6) then

The code will work for any subnet as long as it is in the form 255.255.255.0, 255.255.0.0, 255.0.0.0 and you adjust the relevant parts to suite, otherwise the code that does the comparison gets more complex.

:D

Link to comment
Share on other sites

Yes these machines are going into an AD enviroment. I use a unattended ris image, then a bunch scripts and batch files call up reg mods, tweaks, and common apps. Then machine renames, reboots, autologon one more time, join domain, reboot and ready for end user. Group policy would then push out any individual requested apps.

Link to comment
Share on other sites

Well tried it in a working enviroment, and will not set the name. It does find the serial number, but when it echos the new name, its blank.

Here is a chink of the code, am i missing something?

'Local Computer
strComputer = "."

'Change next lines for relevant subnets
AMSsubnet = "10.2.144"
ATLsubnet = "10.2.20"
BARsubnet = "10.2.145"
BEIsubnet = "10.2.228"
BERsubnet = "10.2.146"
BRUsubnet = "10.2.147"
CRPsubnet = "129.9.11"


Set shell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
Set sh = CreateObject("WScript.Shell")

Dim colSettingsComp : Set colSettings = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
Dim colSettingsBios : Set colSettingsBios = objWMIService.ExecQuery("Select * from Win32_BIOS")
Dim objComputer, strSerial

'Uncomment next three lines if you want Computer Model
'For Each objComputer in colSettings
' strModel = objComputer.Model
'Next

For Each objComputer in colSettingsBios
strSerial = objComputer.SerialNumber
Next

'next line to display Computer serial number if found - test purposes only
wscript.echo strSerial

'test which subnet computer IP matches then append site code to beginning of serial number
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
'Change the number 9 on line below to match the total number of characters in your subnet - dont include the zero example is 255.255.255.0
if AMSsubnet = LEFT(IPConfig.IPAddress(i),9) then
CompName = "AMS" + strSerial

Elseif ATLsubnet = LEFT(IPConfig.IPAddress(i),9) then
CompName = "ATL" + strSerial

Elseif BARsubnet = LEFT(IPConfig.IPAddress(i),9) then
CompName = "BAR" + strSerial

Elseif BEIsubnet = LEFT(IPConfig.IPAddress(i),9) then
CompName = "BEI" + strSerial

Elseif BERsubnet = LEFT(IPConfig.IPAddress(i),9) then
CompName = "BER" + strSerial

Elseif BRUsubnet = LEFT(IPConfig.IPAddress(i),9) then
CompName = "BRU" + strSerial

Elseif CRPsubnet = LEFT(IPConfig.IPAddress(i),9) then
CompName = "CRP" + strSerial


end if
End If
Next

'Change computer name to new one - change wont ask for reboot
Shell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", CompName
Shell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", CompName

Link to comment
Share on other sites

well i have had a quick look at the script.... and you havent changed the number that this line usies.

if AMSsubnet = LEFT(IPConfig.IPAddress(i),9) then

the number nine is used to strip off the unwanted digits from the IP address (host portion), therefore it needs to be equal to the total number of digits including the "." of the network portion of the IP address used on this line

AMSsubnet = "10.2.144"

ie

AMSsubnet is 8

ATLsubnet is 7

Otherwise the conditions used in the elsif lines will never be true and therefore blank name!

Hope this sorts it

Link to comment
Share on other sites

Oh, i had those numbers comfused with the subnet mask (255.255.255.0=9)

I was also playing with some code from MS and it loks like i may be missing something when it's getting the ip.

from them:

strComputer = "."

Set objWMIService = GetObject( _

"winmgmts:\\" & strComputer & "\root\cimv2")

Set IPConfigSet = objWMIService.ExecQuery _

("Select IPAddress from Win32_NetworkAdapterConfiguration ")

For Each IPConfig in IPConfigSet

If Not IsNull(IPConfig.IPAddress) Then

For i=LBound(IPConfig.IPAddress) _

to UBound(IPConfig.IPAddress)

WScript.Echo IPConfig.IPAddress(i)

Next

End If

Next

I dont have the For i=.......on, which I think I need if i am comparing (IPConfig.IPAddress(i) right?

edit: it worked on my test box, but still have a question about this.

if I have subnet 10.2.162 and another one is 10.2.163, both are an 8 count. Will is still know what 3 letter site to assign, or will it just grab the first 8 count suubet?

I am so confused :wacko:

Edited by mls15000
Link to comment
Share on other sites

If you're using RIS have you considered simply prestaging the workstations? One benefit is that you can avoid a lot of this scripting when it isn't really necessary and you can also put the workstation in the appropriate OU, again without scripting. Don't get me wrong, I'm a VBscript freak of the highest order. It just doesn't seem appropriate here.

If anyone is interested, here is a script I wrote to change the name of the computer to PCYYMMDDHHMM. The italics denoting year, month, day, etc of when the operating system was installed. I tend to use this strictly in NON domain environments.

Sub RenameComp
Dim strName, strComputer, objWMIService, colComputers, objComputer, Err
strName = "PC" & Year(Date) & Month(Date) & Day(Date) & Hour(Time) & Minute(Time)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
    Err = ObjComputer.Rename(strName)
Next
End Sub

Link to comment
Share on other sites

I have considered pre-staging, but management might now want to deploy with SMS or another tool. So i am keeping my options open in case I cant pre-stage.

Well, after changing all the subnet entries, it no longer works again. :realmad:

Edited by mls15000
Link to comment
Share on other sites

if I have subnet 10.2.162 and another one is 10.2.163, both are an 8 count. Will is still know what 3 letter site to assign, or will it just grab the first 8 count suubet?

The answer is yes!

When the script retrieves the ip address of the local puter in question this number (8, 7 etc) is used to get only the digits of th ip address you are interested in (network portion). This is why the conditions used will only work with basic subnets(255.255.255.0 - 255.255.0.0 etc). more complex conditions would have to be applied if a class C ip address was subnetted by say 255.255.255.224.

this line

if AMSsubnet = LEFT(IPConfig.IPAddress(i),9) then

uses the number 9 in this case to count from the left of the value contianed in IPAddress (eg 192.168.1.254) that number of places to the right (192.168.1) and compare the result to AMSsubnet if they are equal then carry out the next lines.

Hope this makes it clear

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...