mls15000 Posted July 25, 2005 Share Posted July 25, 2005 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 lineWScript.Echo "The raw serial number is" & strSerialNumber' --- Add leading Alpha characters to avoid DNS confusionstrSerialNumber= "site" & strSerialNumber' --- Build Command Line, to check out the command line uncommentstrCommandLine = PATH_TO_WSNAME & " /N:" & strSerialNumber & " " & WSNAME_PARAMETERSWScript.Echo "About to run the following command:" & vbCRLF & vbCRLF & strCommandLine' --- Now call WSName with the serial numberCall 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 IfTried to add this into the mix,but no luck:subnetsubnett = subnetIf subnett = "10.2.144" then site = "AMS" elseif subnett = "10.2.20" then site = "ATL"end if Link to comment Share on other sites More sharing options...
berrick Posted July 25, 2005 Share Posted July 25, 2005 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 ComputerstrComputer = "."'Change next two lines for relevant subnetsAMSsubnet = "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'NextFor Each objComputer in colSettingsBios strSerial = objComputer.SerialNumberNext'next line to display Computer serial number if found - test purposes onlywscript.echo strSerial'test which subnet computer IP matches then append AMS/ATL to beginning of serial numberFor 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 IfNext'Change computer name to new one - change wont ask for reboot WSHShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", CompNameWSHShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", CompNamejust cut 'n' paste in to a text doc then rename to "wotever" with a .vbs extenstion.Hope this helps Link to comment Share on other sites More sharing options...
mls15000 Posted July 25, 2005 Author Share Posted July 25, 2005 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 More sharing options...
jcarle Posted July 25, 2005 Share Posted July 25, 2005 Valid subnets can only contain 0 - 255 for each byte. Link to comment Share on other sites More sharing options...
berrick Posted July 25, 2005 Share Posted July 25, 2005 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 More sharing options...
mls15000 Posted July 26, 2005 Author Share Posted July 26, 2005 (edited) Yes, all my subnet masks are 255.255.255.0I tested with a virtual machine and after a little tweaking, it seems like it will do the trick.Was getting error on these linesWSHShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", CompNameWSHShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", CompNameI added this to the beginning of the scriptSet shell = CreateObject("WScript.Shell")Then changed the trouble lines toShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", CompNameShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", CompNameScrit 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) thenI 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 July 26, 2005 by mls15000 Link to comment Share on other sites More sharing options...
berrick Posted July 26, 2005 Share Posted July 26, 2005 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 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 questionAMSsubnet = "192.168.0"DMZsubnet = "10.1.1"and add the follow for each networkElseif DMZsubnet = LEFT(IPConfig.IPAddress(i),6) thenThe 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. Link to comment Share on other sites More sharing options...
mls15000 Posted July 26, 2005 Author Share Posted July 26, 2005 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 More sharing options...
mls15000 Posted July 26, 2005 Author Share Posted July 26, 2005 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 ComputerstrComputer = "."'Change next lines for relevant subnetsAMSsubnet = "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'NextFor Each objComputer in colSettingsBiosstrSerial = objComputer.SerialNumberNext'next line to display Computer serial number if found - test purposes onlywscript.echo strSerial'test which subnet computer IP matches then append site code to beginning of serial numberFor Each IPConfig in IPConfigSetIf 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) thenCompName = "AMS" + strSerialElseif ATLsubnet = LEFT(IPConfig.IPAddress(i),9) thenCompName = "ATL" + strSerialElseif BARsubnet = LEFT(IPConfig.IPAddress(i),9) thenCompName = "BAR" + strSerialElseif BEIsubnet = LEFT(IPConfig.IPAddress(i),9) thenCompName = "BEI" + strSerialElseif BERsubnet = LEFT(IPConfig.IPAddress(i),9) thenCompName = "BER" + strSerialElseif BRUsubnet = LEFT(IPConfig.IPAddress(i),9) thenCompName = "BRU" + strSerialElseif CRPsubnet = LEFT(IPConfig.IPAddress(i),9) thenCompName = "CRP" + strSerialend ifEnd IfNext'Change computer name to new one - change wont ask for reboot Shell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", CompNameShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", CompName Link to comment Share on other sites More sharing options...
berrick Posted July 26, 2005 Share Posted July 26, 2005 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) thenthe 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 lineAMSsubnet = "10.2.144"ieAMSsubnet is 8 ATLsubnet is 7Otherwise 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 More sharing options...
mls15000 Posted July 26, 2005 Author Share Posted July 26, 2005 (edited) 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 IfNextI 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 Edited July 26, 2005 by mls15000 Link to comment Share on other sites More sharing options...
RogueSpear Posted July 26, 2005 Share Posted July 26, 2005 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) NextEnd Sub Link to comment Share on other sites More sharing options...
mls15000 Posted July 26, 2005 Author Share Posted July 26, 2005 (edited) 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. Edited July 26, 2005 by mls15000 Link to comment Share on other sites More sharing options...
berrick Posted July 27, 2005 Share Posted July 27, 2005 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 lineif AMSsubnet = LEFT(IPConfig.IPAddress(i),9) thenuses 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 More sharing options...
mls15000 Posted July 27, 2005 Author Share Posted July 27, 2005 Sure does, thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now