All Forums >> [Scripting] >> WSH & Client Side VBScript >> RE: Pull information from a file based on search criteria Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Sordoff, I am trying to create dhcp reservation for all machines on our network. However if you query the DHCP Server it is only giving me list of machines that are currently connected.(incomplete) So you willl get a text file that will just skip over the ips that are currently not in use.
So I was trying to create a script that would loop through all the available IP's 0 to 255. If the IP does not exist on DHCP Server text file then create a specialized entry.
I want to execute the Netsh command (Create Reservations) for all the IPs in the range I provide 0-255. But I want the script to pull the Reserved IP address and the MAC address and client name from the from the text file.
....Will the netsh commands for *10 and *.13 the same? If not - how would you like to specify the different params No I want it to pull the data required from the text file.
PULL THE DATA FROM THE TEXT FILE FOR NETSH COMMAND
strScope = "100.10.9.0" strReservedIP = "100.10.9.10" strMAC = "000000000010" strClientName = "Machine Name10" strServer = "DHCP Server" ' leave blank for local server
strScope = "100.10.9.0" strReservedIP = "100.10.9.13" strMAC = "000000000013" strClientName = "Machine Name13" strServer = "DHCP Server" ' leave blank for local server
For IP 100.10.9.14 I want to netsh to enter this (tHIS WOULD BE AN ENTRY NOT IN THE TEXT FILE BUT IP WOULD BE IN THE RANGE.
strScope = "100.10.9.0" strReservedIP = "100.10.9.14" strMAC = "000000000000" strClientName = "Machine NameUnknown" strServer = "DHCP Server" ' leave blank for local server
one way to make it happen: start with this script netshrun.vbs
Now take the main problem "run netsh for a list of IPs" and split it into two subproblems:
(a) building the netsh command from suitable parameters
(b) getting these parameters (from your dhcpouts.txt file)
Concentrate on (a). Your netsh command setup:
What about "strClientComment"? Let's assume you need it. Concatenating complex strings like command lines is a bad idea; especially if you have to add quotes and other special chars. An easier and more general way is to use replacement. So add:
(1) ''# bldCmd1 - first try to build netsh command
(2) Case "bldcmd1" nRVal = bldCmd1()
(3) ' ############################################################################ ''# bldCmd1 - first try to build netsh command ' ############################################################################
Function bldCmd1() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, bldCmd1() was called." bldCmd1 = nRVal End Function
and run:
cscript netshrun.vbs bldCmd1 ok, bldCmd1() was called.
Taking advantage of the fact that the last version of a sequence of Subs/Functions with the same name and parameter list 'wins', we can either put an enhanced version of bldCmd1() in the script or - in a more orderly fashion - setup our embellished function as bldCmd2(). Ok, I'm in a hurry:
Of course we need the functions used in bldCmd1():
Output:
For the next step, we make dicParms (now called gdicDefParms) with suitable values and sCmd (=> gsCmd) globally available. Furthermore we add a testCmd() func to make sure we can do the main job if get the params right.
I post the whole (current version of the) script
Now for the second problem:
getting these parameters (from your dhcpouts.txt file)
Start with a simple version of the parseDHCP() function:
Function parseDHCP() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, parseDHCP() was called." parseDHCP = nRVal End Function
Check output:
cscript netshrun.vbs parsedhcp ok, parseDHCP() was called.
Use - a slightly modified version of - ebgreen's code to work thru ".\dhcpouts.txt". First simple version:
Version with dictionary:
Now copy the code of parseDHCP() to an independent function to make it reusable:
I admit a bit of editing was necessary. To test it, add a first version of cmdFound:
[/code] Function cmdFound() Dim nRVal : nRVal = 1 ' assume bad WScript.Echo "ok, cmdFound() was called."
Dim strDHCPFile : strDHCPFile = ".\DHCPOuts.txt" Dim dicClients : Set dicClients = parseDHCPebgreen( strDHCPFile )
Dim sKeyClient, dicParams For Each sKeyClient In dicClients WScript.Echo "-------------", sKeyClient, "-------------" Next
As for the two issues that you raised with my script, both issues were based on the samples posted by the op. I used his sample text which did have "Client : ", and I used his loop which looped IPs other than what was in his sample (which I pointed out to him in a later post).
I'm grateful for your choice of words. (I expected - and deserve - pedantic; but I can't help it). It wasn't my intention to put any blame on you; I just wanted to avoid additional problems caused by such nearly invisible discrepancies.