Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Part 9: Scenario for Adding New Clients

Published: July 12, 2004

In places where network clients frequently join and leave the network, such as universities, hotels, and convention centers, automating changes to network client configuration can reduce manual errors and increase productivity, freeing network administrators to concentrate on more urgent issues.

More sophisticated approaches, such as using Group Policy together with Active Directory, may often be the best solution for handling large-scale configuration of frequently changing hosts. A cheap, quick alternative (or complement), however, might be a script that assigns necessary network settings to client computers that you must add to, update, or remove from a network. Such a script would likely include settings for DHCP (or static IP), DNS, WINS, and gateway information. You could integrate some of the script's functionality into existing logon scripts, run it separately, or have Help Desk staff run it, depending on workflow in the organization.

Depending on the settings changed, you might need to make corresponding changes on DHCP, DNS, and WINS servers, many of which could be automated with scripts or command-line tools. These server-side operations are not covered in this paper.

Although this script handles only the TCP/IP settings for a client, you can easily add other client settings, such as standard network shares or home directories. On networks that use Active Directory, you can expand the script to use ADSI to add or modify computer and user information within the directory.

The Script

This script adds, updates, and removes client computers from a TCP/IP network.

caution.gif  Caution
This script may make changes in your computer configuration. Run it only on a test computer and note the settings involved before running it.

Input File

The script takes a text file as input, the path to which is specified in the script. In this example, the text file is named Clients.csv. CSV stands for comma-separated values, and you can edit a .csv file in a spreadsheet as well as a text editor. Using a spreadsheet can make creating and editing this file much easier.

Each line of the file contains the following parameters separated by commas:

hostname, add or remove client, IP address allocation method, IP address, subnet mask, default gateway, default gateway cost metric, DNS changes, DNS server search order, DNS hostname, DNS domain, DNS domain suffix search order, full DNS registration enabled, domain DNS registration enabled, WINS changes, NetBIOS TCP/IP options, DNS enabled for WINS resolution, WINS LMHOST lookup enabled, WINS HOST lookup file, WINS scope ID, WINS primary server, WINS secondary server

For the Clients.csv input file, Table 47 lists the file’s parameters and their type.

Table 47   Parameters for Clients.csv Input File

Parameters (in order)

Type

Hostname

String

Add or remove client1

String

IP address allocation method2

String

IP address

String array

Subnet mask

String array

Default gateway

String array

Default gateway cost metric

Integer array

DNS changes

Boolean

DNS server search order

String array

DNS hostname

String

DNS domain

String

DNS domain suffix search order

String array

Full DNS registration enabled3

Boolean

Domain DNS registration enabled4

Boolean

WINS changes

Boolean

NetBIOS TCP/IP options

Integer

DNS enabled for WINS resolution

Boolean

WINS LMHOST lookup enabled

Boolean

WINS HOST lookup file

String

WINS scope ID

String

WINS primary server

String

WINS secondary server

String

1 If this parameter is set to remove, the third parameter, IP address allocation method, must be left blank; the eighth parameter, DNS changes, must be set to False or left blank; and the fifteenth parameter, WINS changes, must be set to False or left blank.

2 If this parameter is set to DHCP, the next four parameters (IP address, subnet mask, default gateway, and gateway cost metric) should be empty and marked only by the comma delimiter. If the IP address allocation method is set to static IP addressing, you must set these four parameters; and if the computer will connect to a network using DNS, you must also set the DNS server search order with the IP address of at least one DNS server.

3 If the full DNS registration enabled parameter is not set, the following parameter (domain DNS registration enabled) is ignored.

4 The domain DNS registration enabled parameter has meaning only if the previous parameter (full DNS registration enabled) is true.


For any parameters that are not set, a comma (not preceded by a space) must be used. The final line must not be terminated with an Enter character, which would be read by the script as an empty entry. Note that the following example contains only four lines terminated by a carriage return/line feed: those lines begin with "client1," "client2," "newclient1," and "oldclient1," which are listed in boldface type for legibility in this example. The second and third apparent lines are continuations of the first, which is not ended by a carriage return/line feed. The actual second line begins with "client2."

The script assigns the IP address allocation method for each host that is not to be removed. If the method is static IP addressing, the script also assigns the next four parameters and the DNS server search order. If the method is DHCP, the script does not assign any other parameters.



client1,,staticip,192.168.0.13,255.255.255.0,192.168.0.1,1,True,192.168.0.1 192.168.0.2,peter-d2,
fabrikam.com,hr.fabrikam.com it.fabrikam.com,True,True,True,1,,,,,,
client2,,dhcp,,,,,False,,,,,,,False,,,,,,,
newclient1,add,staticip,192.168.0.13,255.255.255.0,192.168.0.1,1,True,
192.168.0.1 192.168.0.2,peter-d2,fabrikam.com,hr.fabrikam.com it.fabrikam.com,True,True,True,1,,,,,,
oldclient1,remove,,,,,,False,,,,,,,False,,,,,,,



Scripting Steps

  1. Define constants for use with the FileSystemObject.

  2. Create variables to hold the paths of the input and output files and counters for the final tally.

  3. Call the GetInput function, passing it the path to the input file.

  4. Use the VBScript Split function to divide the returned file contents into an array of which each element contains a line of the text file, using the line break character as the delimiter.

  5. Check for the existence of the specified output file. If it exists, open it for appending so the contents are not overwritten. If it does not exist, create it and open it.

  6. Write a header for the output file containing the date and the number of computers in the input file.

  7. Iterate through the array of lines from the text file (each representing a client and its settings) and perform the following tasks on each one.

  8. Again using the Split function, divide each line of the text file into another array containing the settings for each host, using the comma as the delimiter.

  9. Read the first element of the new array, the host name, into a variable and display it.

  10. Call the PingClient function, passing it the host name.

  11. If the ping succeeds, connect to the WMI service on the host.

  12. If no error occurs in connecting, assign the remaining 21 elements of the new array to variables representing the settings for this host.

  13. Write a heading for this host to the text file.

  14. Call the GetSettings subroutine to retrieve the current settings of this host and write them to the output file.

  15. Check the second parameter from the input file for this host to determine whether TCP/IP settings are to be updated or whether this is a client to be added or removed. Write an appropriate header and increment the counter for clients added, removed, or updated.

  16. If the network client is to be removed, call the RemoveClient subroutine.

  17. Check the parameter for IP allocation to determine if DHCP or static IP settings are to be changed. For DHCP, call the SetDHCP function. For static IP, call the SetStaticIP function.

  18. Check the parameter for DNS to determine if DNS settings are to be changed. If so, call the SetDNS subroutine.

  19. Check the parameter for WINS to determine if DNS settings are to be changed. If so, first call the SetNetBIOS function.

  20. If the SetNetBIOS function is able to set NetBIOS over TCP/IP or if the setting is already as preferred, check the value of the NetBIOS over TCP/IP setting. If the value is 1, indicating that NetBIOS over TCP/IP is enabled, but the setting is not retrieved from the DHCP server, call the SetWins function.

  21. Again call the GetSettings subroutine, which requeries Win32_ComputerSystem and Win32_NetworkAdapterConfiguration, retrieves the properties reflecting any changes in settings, and writes them to the output file.

  22. If the script could not connect to WMI on this host, write an error message to the output file.

  23. If the ping to this host failed, write an error message to the output file.

  24. Write a footer to the output file summarizing the number of clients added, removed, and updated and any errors; and close the output file.

  25. The GetInput function takes the path to the input file as a parameter. It checks whether the specified input file exists, and if it does, opens the file and reads its contents. It returns the text stream as a string.

  26. The PingClient function takes the name of a host as a parameter. It uses the Exec method of the WshShell object to run the command-line tool Ping.exe against the host name, parsing the output of the tool to see if the ping was successful.

  27. The GetSettings subroutine writes to the output file a range of TCP/IP client settings.

    • It begins by checking the role of the computer in the domain with the DomainRole property of the Win32_ComputerSystem class.

    • It calls the GetOsVer function to verify the version of the operating system. If the version number returned by GetOsVer is greater than 5.0 (Windows 2000), the PartOfDomain property is available and the function retrieves it.

    • Using the values of these global properties, the function writes the computer name, domain, and domain role to the output file.

    • The function iterates through the instances of the Win32_NetworkAdapterConfiguration class for which IP is enabled and writes settings for IP allocation, DNS, NetBIOS, and WINS to the output file.

  28. The RemoveClient subroutine removes a host from the network by checking the current IP allocation method for the client:

    • If the client is using static IP addressing, the subroutine calls the EnableStatic method of Win32_NetworkAdapterConfiguration and assigns an IP address of 0.0.0.0 and a subnet mask of 255. 255. 255. 255.

    • If the client is using DHCP, the subroutine calls the ReleaseDHCPLease method of Win32_NetworkAdapterConfiguration to release the DHCP lease, and then calls the EnableStatic method of Win32_NetworkAdapterConfiguration and assigns an IP address of 0.0.0.0 and a subnet mask of 255. 255. 255. 255.

  29. The SetDHCP subroutine calls the EnableDHCP method of Win32_NetworkAdapterConfiguration to enable DHCP on each IP-enabled network adapter.

  30. The SetStaticIP subroutine calls the EnableStatic method of Win32_NetworkAdapterConfiguration to disable DHPC and enable a static IP address and subnet mask by using the input file parameters of each IP-enabled network adapter. If a static IP address can be assigned, the subroutine then calls the SetGateways method of Win32_NetworkAdapterConfiguration to assign default gateways and gateway cost metrics from the input file to each IP-enabled network adapter.

  31. The SetDNS subroutine assigns new DNS settings to the host. It first checks whether a new DNS host name is assigned in the input file.

    • If a new DNS host name is assigned, it calls the EnableDNS method of Win32_NetworkAdapterConfiguration and passes it parameters for DNS host name, DNS domain, DNS server search order, and DNS domain suffix search order.

    • If no new DNS host name is to be assigned to the computer, the subroutine calls the SetDNSDomain, SetDNSServerSearchOrder, and SetDNSSuffixSearchOrder methods of Win32_NetworkAdapterConfiguration, passing them parameters from the input file for these settings.

    • Whether or not the computer has been assigned a new DNS host name, the subroutine calls the SetDynamicDNSRegistration method of Win32_NetworkAdapterConfiguration, passing it parameters from the input file for dynamic DNS registration settings.

  32. The SetNetbios function changes the NetBIOS over TCP/IP setting for the host. If the current TcpipNetbiosOptions property of the host is not equal to the setting in the input file, the function calls the SetTCPIPNetBIOS method of Win32_NetworkAdapterConfiguration, passing it the parameter from the input file for this setting.

  33. The SetWins function changes the WINS settings on the host. It calls the EnableWINS method of Win32_NetworkAdapterConfiguration, passing it parameters from the input file for enabling DNS for WINS resolution, enabling LMHosts lookup, the name of the host lookup file, and WINS scope ID. Then the function attempts to set the WINS servers for the host by calling the SetWINSServer method of Win32_NetworkAdapterConfiguration and passing it the IP addresses of the primary and secondary WINS servers from the input file.

  34. The GetOsVer function retrieves the three left-most characters of the Version property of the Win32_OperatingSystem class. This floating-point number is used by other code modules to determine the operating system version, which affects whether particular functionality is available to the script.

Listing 63   Addclients.vbs

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
100
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679



On Error Resume Next
 
' Constants for FileSystemObject
Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8
 
' Paths to input and output files. Input file must be a comma-delimited text
' file on same machine where script is to be run with name of accessible
' machine and other settings on each line.
strFileInput = "c:\scripts\clients.csv"
strFileOutput = "c:\scripts\newclients.csv"
 
' Counters for final tally
intAdd = 0
intUpdate = 0
intRemove = 0
intError = 0
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strInput = GetInput(strFileInput)
 
' Get computer info by breaking input stream into array at line breaks.
arrClients = Split(strInput, VbCrLf)
 
' Check to see if the output file exists. If so, open it for appending.
' If not, create it and open it for writing. 
If objFSO.FileExists(strFileOutput) Then
  Set objOutputFile = objFSO.OpenTextFile(strFileOutput, FOR_APPENDING)
Else
  Set objOutputFile = objFSO.CreateTextFile(strFileOutput)
End If
If Err <> 0 Then
  Wscript.Echo "Unable to open " & strFileOutput & " for output."
  WScript.Quit
End If
 
' Write header for file.
objOutputFile.WriteLine "Date: " & Now & VbCrLf & "Adding updating or " & _
 "removing " & (UBound(arrClients) + 1) & " new client computers." & _
 VbCrLf & VbCrLf & String(120, "-") & VbCrLf
 
For Each strClient In arrClients
 
' Get settings by breaking each computer info array at commas.
  strComputer = ""
  arrParams = Split(strClient, ",")
  strComputer = arrParams(0)
  WScript.Echo VbCrLf & "Host: " & strComputer & VbCrLf
 
' Ping remote computer. If no response, display error message and end script.
  blnPingSuccess = PingClient(strComputer)
  If blnPingSuccess = True Then
 
' Connect to the WMI service.
    Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    If Err = 0 Then
 
      Err.Clear
      strAddRemove = arrParams(1)
      strIpAlloc = arrParams(2)
      arrIPAddresses = Split(arrParams(3))
      arrSubnetMasks = Split(arrParams(4))
      arrDefaultGateways = Split(arrParams(5))
      arrGatewayCostMetrics = Split(arrParams(6))
      blnDNSChanges = arrParams(7)
      arrDNSServerSearchOrder = Split(arrParams(8))
      strDNSHostName = arrParams(9)
      strDNSDomain = arrParams(10)
      arrDNSDomainSuffixSearchOrder = Split(arrParams(11))
      blnFullDNSRegistrationEnabled = arrParams(12)
      blnDomainDNSRegistrationEnabled = arrParams(13)
      blnWINSChanges = arrParams(14)
      intNetbiosTcpip = arrParams(15)
      blnDNSEnabledForWINSResolution = arrParams(16)
      blnWINSEnableLMHostsLookup = arrParams(17)
      strWINSHostLookupFile = arrParams(18)
      strWINSScopeID = arrParams(19)
      strWINSPrimaryServer = arrParams(20)
      strWINSSecondaryServer = arrParams(21)
 
' Write header for each computer.
      objOutputFile.WriteLine "Host: " & strComputer & VbCrLf & VbCrLf & _
       ",Settings before changes:" & VbCrLf
 
' Get current settings and write to output file.
      GetSettings
      objOutputFile.WriteLine VbCrLf & "," & String(60, "-")
 
      If LCase(strAddRemove) = "add" Then
        objOutputFile.WriteLine VbCrLf & ",TCP/IP client change mode" & _
         VbCrLf & VbCrLf & ",Client to be added to network. " & VbCrLf & _
         ",Administrator must make corresponding changes on servers."
        intAdd = intAdd + 1
      ElseIf LCase(strAddRemove) = "remove" Then 
        objOutputFile.WriteLine VbCrLf & ",TCP/IP client change mode" & _
         VbCrLf & VbCrLf & ",Client to be removed from network. " & VbCrLf & _
         ",Administrator must make corresponding changes on servers."
        intRemove = intRemove + 1
        RemoveClient
      Else
        objOutputFile.WriteLine VbCrLf & ",TCP/IP client change mode" & _
         VbCrLf & VbCrLf & ",Settings to be updated on existing client. " & _
         VbCrLf & ",Administrator may have to make corresponding changes " & _
         "on servers."
        intUpdate = intUpdate + 1
      End If
 
      If LCase(strIpAlloc) = "dhcp" Then
        SetDHCP
      ElseIf LCase(strIpAlloc) = "staticip" Then 
        SetStaticIP
      Else
        objOutputFile.WriteLine VbCrLf & ",IP allocation changes" & VbCrLf & _
         VbCrLf & ",No changes made in = IP allocation settings."
      End If
 
      If blnDNSChanges Then
        SetDNS
      Else
        objOutputFile.WriteLine VbCrLf & ",DNS changes" & VbCrLf & _
         VbCrLf & ",No changes made in DNS settings."
      End If
 
      objOutputFile.WriteLine VbCrLf & ",NetBIOS & WINS changes" & VbCrLf
      If blnWINSChanges Then
        blnNetbiosSet = SetNetbios
        If blnNetbiosSet = True Then
          Select Case intNetbiosTcpip
            Case 0
              objOutputFile.WriteLine VbCrLf & ",NetBIOS settings from " _
               & "DHCP server used. WINS parameters not set."
            Case 1
              strOutput = VbCrLf & ",NetBIOS over TCP/IP enabled. " _
               & "Setting WINS parameters ..." & VbCrLf
              blnWinsSet = SetWins
              If blnWinsSet = True Then
                strOutput = strOutput & ",WINS enabled and parameters set."
              Else
                strOutput = strOutput & ",Unable to enable WINS and set " & _
                 "parameters."
              End If
              objOutputFile.WriteLine strOutput
            Case 2
              objOutputFile.WriteLine VbCrLf & ",NetBIOS over TCP/IP " & _
               "disabled. WINS parameters not set."
            Case Else
              objOutputFile.WriteLine VbCrLf & ",Could not determine " & _
               "setting for NetBIOS Over TCP/IP. WINS parameters not set."
            End Select
        Else
          objOutputFile.WriteLine ",NetBIOS Over TCP/IP setting does " & _
           "not require setting WINS parameters."
        End If
      Else
          objOutputFile.WriteLine ",No changes made in NetBIOS & WINS " & _
           "settings."
      End If
 
      objOutputFile.WriteLine VbCrLf & "," & String(60, "-") & VbCrLf & _
       VbCrLf & ",Settings after changes:" & VbCrLf
      GetSettings
      objOutputFile.WriteLine VbCrLf & String(120, "-") & VbCrLf
 
    Else
 
      objOutputFile.WriteLine "Host: " & strComputer & VbCrLf _
       & VbCrLf & ",Error connecting to WMI service." & VbCrLf & _
       ",Error Number: " & Err.Number & VbCrLf & _
       ",Error Source: " & Err.Source & VbCrLf & _
       ",Error Description: " & Err.Description & VbCrLf & VbCrLf & _
       String(120, "-") & VbCrLf
      Err.Clear
      intError = intError + 1
 
    End If
 
  Else
 
    objOutputFile.WriteLine "Host: " & strComputer & VbCrLf _
     & VbCrLf & ",Unable to connect." & VbCrLf & VbCrLf & _
     String(120, "-") & VbCrLf
    intError = intError + 1
 
  End If
 
Next
 
' Write footer for file and close.
objOutputFile.WriteLine "Summary: " & VbCrLf & _
 "Added " & intAdd & " new client computers." & VbCrLf & _
 "Updated " & intUpdate & " client computers." & VbCrLf & _
 "Removed " & intRemove & " client computers." & VbCrLf & _
 "Unable to connect to or bind to WMI service on " & intError & _
 " client computers." & VbCrLf & VbCrLf & String(64, "=") & VbCrLf
 
objOutputFile.Close
 
WScript.Echo VbCrLf & "Data written to " & strFileOutput & "."
 
'******************************************************************************
 
Function GetInput(strFileInput)
 
' Check to see if the input file exists.
' If so, dump contents of input file into a string.
 
If objFSO.FileExists(strFileInput) Then
  Set objInputFile = objFSO.GetFile(strFileInput)
  If objInputFile.Size > 0 Then
    Set objInputFile = objFSO.OpenTextFile(strFileInput, FOR_READING)
    strInputStream = objInputFile.ReadAll
    objInputFile.Close
    GetInput = strInputStream
  Else
    Wscript.Echo strFileInput & " is empty."
    WScript.Quit
  End If
Else
  WScript.Echo strFileInput & " does not exist on this computer."
  WScript.Quit
End If
 
End Function
 
'******************************************************************************
 
Function PingClient(strComputer)
 
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
  PingClient = True
Else
  PingClient = False
End If
 
End Function
 
'******************************************************************************
 
Sub GetSettings
 
Set colCompSystems = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_ComputerSystem")
 
' Write header for each computer.
  For Each objCompSystem in colCompSystems
    intDomainRole = objCompSystem.DomainRole
    Select Case intDomainRole
      Case 0 strDomainRole = "Standalone Workstation"
      Case 1 strDomainRole = "Member Workstation"
      Case 2 strDomainRole = "Standalone Server"
      Case 3 strDomainRole = "Member Server"
      Case 4 strDomainRole = "Backup Domain Controller"
      Case 5 strDomainRole = "Primary Domain Controller"
      Case Else strDomainRole = "Unable to determine domain role."
    End Select
    sngOsVer = GetOsVer
    If sngOsVer > 5 Then
      If objCompSystem.PartOfDomain Then
        strDomain = ",Domain:,," & objCompSystem.Domain
      Else
        strDomain = ",Workgroup:,," & objCompSystem.Workgroup
      End If
    Else
      strDomain = ",Domain or Workgroup:,," & objCompSystem.Domain
    End If
    objOutputFile.WriteLine ",Computer Name:,," & LCase(objCompSystem.Name) _
     & VbCrLf & strDomain & VbCrLf & ",Domain Role:,," & strDomainRole
  Next
 
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
For Each objNicConfig In colNicConfigs
 
' Output network adapter headings
 
  intIndex = objNicConfig.Index
  Set objNic = objWMIService.Get("Win32_NetworkAdapter.DeviceID=" & intIndex)
 
  strAdapterType = objNic.AdapterType
  If IsEmpty(strAdapterType) Or IsNull(strAdapterType) Then
    strAdapterType = "Network"
  End If
 
  If sngOsVer > 5 Then
    strNetConn = objNic.NetConnectionID
  Else
    strNetConn = intIndex
  End If
 
  strRow = VbCrLf & "," & strAdapterType & " Adapter " & strNetConn & _
   VbCrLf & ",,Index " & intIndex & VbCrLf & _
   ",," & objNicConfig.Description & VbCrLf
  objOutputFile.WriteLine strRow
 
' Output IP allocation settings
    strRow = ",,DHCP Enabled:            " & objNicConfig.DHCPEnabled & _
     VbCrLf & ",,DHCP Server: " & objNicConfig.DHCPServer & VbCrLf
    strIPAddresses = ""
    If Not IsNull(objNicConfig.IPAddress) Then
      For Each strIPAddress In objNicConfig.IPAddress
        strIPAddresses = strIPAddresses & VbCrLf & ",,,," & strIPAddress
      Next
    End If
    strRow = strRow & ",,IP Addresses: " & strIPAddresses & VbCrLf
    strIPSubnets = ""
    If Not IsNull(objNicConfig.IPSubnet) Then
      For Each strIPSubnet In objNicConfig.IPSubnet
        strIPSubnets = strIPSubnets & VbCrLf & ",,,," & strIPSubnet
      Next
    End If
    strRow = strRow & ",,Subnet Masks: " & strIPSubnets & VbCrLf
    strDefaultIPGateways = ""
    If Not IsNull(objNicConfig.DefaultIPGateway) Then
      For Each strDefaultIPGateway In objNicConfig.DefaultIPGateway
        strDefaultIPGateways = strDefaultIPGateways & VbCrLf & _
         ",,,," & strDefaultIPGateway
      Next
    End If
    strRow = strRow & ",,Default Gateways: " & strDefaultIPGateways & VbCrLf
    strGatewayCostMetrics = ""
    If Not IsNull(objNicConfig.GatewayCostMetric) Then
      For Each strGatewayCostMetric In objNicConfig.GatewayCostMetric
        strGatewayCostMetrics = strGatewayCostMetrics & VbCrLf & _
         ",,,," & strGatewayCostMetric
      Next
    End If
    strRow = strRow & ",,Gateway Cost Metrics: " & strGatewayCostMetrics
    objOutputFile.WriteLine strRow
 
' Output DNS settings
    strDNSServerSO = ""
    If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
      For Each strDNSServer In objNicConfig.DNSServerSearchOrder
        strDNSServerSO = strDNSServerSO & VbCrLf & ",,,," & strDNSServer
      Next
    End If
    strRow = ",,DNS Server Search Order:,," & strDNSServerSO & VbCrLf & _
     ",,DNS Domain:,," & objNicConfig.DNSDomain & VbCrLf
    strDNSDomainSuffixes = ""
    If Not IsNull(objNicConfig.DNSDomainSuffixSearchOrder) Then
      For Each strDNSDomainSuffix In objNicConfig.DNSDomainSuffixSearchOrder
        strDNSDomainSuffixes = strDNSDomainSuffixes & strDNSDomainSuffix & ",,"
      Next
    End If
    strRow = strRow & ",,DNS Domain Suffix Search Order:,," & _
     strDNSDomainSuffixes & VbCrLf & ",,Domain DNS Registration Enabled: " & _
     objNicConfig.DomainDNSRegistrationEnabled & VbCrLf & _
     ",,Full DNS Registration Enabled: " & _
     objNicConfig.FullDNSRegistrationEnabled
    objOutputFile.WriteLine strRow
 
' Output WINS & NetBIOS settings
    intNetBIOS = objNicConfig.TcpipNetbiosOptions
    Select Case intNetBIOS
      Case 0 strNetBIOS = "Use NetBIOS setting from the DHCP server"
      Case 1 strNetBIOS = "Enable NetBIOS over TCP/IP"
      Case 2 strNetBIOS = "Disable NetBIOS over TCP/IP"
    End Select
    strRow = ",,NetBIOS Over TCP/IP: " & strNetBIOS & VbCrLf & _
     ",,Primary WINS Server: " & objNicConfig.WINSPrimaryServer & VbCrLf & _
     ",,Secondary WINS Server: " & objNicConfig.WINSSecondaryServer & _
     VbCrLf & ",,DNS Enabled For WINS Resolution: " & _
     objNicConfig.DNSEnabledForWINSResolution & VbCrLf & _
     ",,WINS Enable LMHosts Lookup: " & _
     objNicConfig.WINSEnableLMHostsLookup & VbCrLf & _
     ",,WINS Host Lookup File: " & _
     objNicConfig.WINSHostLookupFile & VbCrLf & _
     ",,WINS Scope ID: " & objNicConfig.WINSScopeID
    objOutputFile.WriteLine strRow
 
Next
 
End Sub
 
'******************************************************************************
 
Sub RemoveClient
 
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
objOutputFile.WriteLine VbCrLf & ",Removing client from network ..."
 
For Each objNicConfig In colNicConfigs
  objOutputFile.WriteLine VbCrLf & ",Network Adapter " & objNicConfig.Index & _
   VbCrLf & ",," & objNicConfig.Description & VbCrLf
  If Not objNicConfig.DHCPEnabled Then
    strRow = ",,Attempting to set IP address to 0.0.0.0 ..."
    intStaticRet = objNicConfig.EnableStatic("0.0.0.0", "255.255.255.255")
    If intStaticRet = 0 Then
      strSuccess = ",,,Set static IP address to 0.0.0.0."
    Else
      strSuccess = ",,,Unable to set static IP address."
    End If
    objOutputFile.WriteLine strRow & VbCrLf & strSuccess
  Else
    intReleaseRet = objNicConfig.ReleaseDHCPLease
    If intReleaseRet = 0 Then
      strSuccess = ",,,DHCP lease successfully released."
      intStaticRet = objNicConfig.EnableStatic("0.0.0.0", "255.255.255.255")
      If intStaticRet = 0 Then
        strSuccess = strSuccess  & " Set static IP address to 0.0.0.0."
      Else
        strSuccess = strSuccess  & " Unable to set static IP address."
      End If
    Else
      strSuccess = ",,,Unable to release DHCP lease."
    End If
    objOutputFile.WriteLine strRow & VbCrLf & strSuccess
  End If
Next
 
End Sub
 
'******************************************************************************
 
Sub SetDHCP
 
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
objOutputFile.WriteLine VbCrLf & ",DHCP changes"
 
For Each objNicConfig In colNicConfigs
  objOutputFile.WriteLine VbCrLf & ",Network Adapter " & objNicConfig.Index & _
   VbCrLf & ",," & objNicConfig.Description & VbCrLf
  If Not objNicConfig.DHCPEnabled Then
    strRow = ",,Attempting to enable DHCP ..."
    intEnableDHCP = objNicConfig.EnableDHCP
    If intEnableDHCP = 0 Then
      strSuccess = ",,,DHCP successfully enabled."
    Else
      strSuccess = ",,,Unable to enable DHCP."
    End If
    objOutputFile.WriteLine strRow & VbCrLf & strSuccess
  Else
    objOutputFile.WriteLine strRow & ",,DHCP already enabled"
  End If
Next
 
End Sub
 
'******************************************************************************
 
Sub SetStaticIP
 
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
objOutputFile.WriteLine VbCrLf & ",Static IP changes"
 
For Each objNicConfig In colNicConfigs
  objOutputFile.WriteLine VbCrLf & ",Network Adapter " & objNicConfig.Index & _
   VbCrLf & ",," & objNicConfig.Description & VbCrLf
  If objNicConfig.DHCPEnabled Then
    strRow = ",,Attempting to disable DHCP and enable static IP ..." & VbCrLf
  Else
    strRow = ",,Static IP addressing already enabled. Changing " & _
     "static IP settings ..." & VbCrLf
  End If
  If (Not(IsNull(arrIPAddresses(0)))) And _
   (Not(IsEmpty(arrIPAddresses(0)))) And _
   (Not(IsNull(arrSubnetMasks(0)))) And _
   (Not(IsEmpty(arrSubnetMasks(0)))) Then
    intStaticReturn = objNicConfig.EnableStatic(arrIPAddresses, arrSubnetMasks)
    If intStaticReturn = 0 Then
      strRow = strRow & ",,DHCP disabled - static IP address enabled." & VbCrLf
      If (Not(IsNull(arrDefaultGateways(0)))) And _
       (Not(IsEmpty(arrDefaultGateways(0)))) And _
       (Not(IsNull(arrGatewayCostMetrics(0)))) And _
       (Not(IsEmpty(arrGatewayCostMetrics(0)))) Then
        Set objNicChanged = objWMIService.Get _
         ("Win32_NetworkAdapterConfiguration.Index=" & objNicConfig.Index)
        intGatewaysReturn = objNicChanged.SetGateways _
         (arrDefaultGateways, arrGatewayCostMetrics)
        If intGatewaysReturn = 0 Then
          strRow = strRow & ",,Assigned new default gateways."
        Else
          strRow = strRow & ",,Unable to assign default gateways."
        End If
      Else
        strRow = strRow & ",,No new default gateway or cost metric " & _
         "listed in input."
      End If
    Else
      strRow = strRow & ",,Unable to enable static IP address."
    End If
  Else
    strRow = strRow & ",,No new static IP address or subnet mask " & _
     "listed in input."
  End If
  objOutputFile.WriteLine strRow
 
Next
 
End Sub
 
'******************************************************************************
 
Sub SetDNS
 
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
objOutputFile.WriteLine VbCrLf & ",DNS Changes"
 
For Each objNicConfig In colNicConfigs
  objOutputFile.WriteLine VbCrLf & ",Network Adapter " & objNicConfig.Index _
   & VbCrLf & ",," & objNicConfig.Description & VbCrLf & VbCrLf & _
   ",,Attempting to make changes in DNS configuration ..."
 
  If (Not(IsNull(strDNSHostName))) And (Not(IsEmpty(strDNSHostName))) Then
  intEnableDNS = objNicConfig.EnableDNS(strDNSHostName, strDNSDomain, _
   arrDNSServerSearchOrder, arrDNSDomainSuffixSearchOrder)
    If intEnableDNS = 0 Then
      objOutputFile.WriteLine ",,Assigned new hostname."
      If (Not(IsNull(strDNSDomain))) And (Not(IsEmpty(strDNSDomain))) Then
        objOutputFile.WriteLine ",,Assigned new domain."
      Else
        objOutputFile.WriteLine ",,No new domain listed in input."
      End If
      If (Not(IsNull(arrDNSServerSearchOrder(0)))) And _
       (Not(IsEmpty(arrDNSServerSearchOrder(0)))) Then
        objOutputFile.WriteLine ",,Assigned new DNS server search order."
      Else
        objOutputFile.WriteLine ",,No new DNS server search order " & _
         "listed in input."
      End If
      If (Not(IsNull(arrDNSDomainSuffixSearchOrder(0)))) And _
       (Not(IsEmpty(arrDNSDomainSuffixSearchOrder(0)))) Then
        objOutputFile.WriteLine ",,Assigned new DNS domain suffix " & _
         "search order."
      Else
        objOutputFile.WriteLine ",,No new DNS domain suffix " & _
         "search order listed in input."
      End If
    Else
      objOutputFile.WriteLine ",,Unable to assign new hostname or enable DNS."
    End If
  Else
    objOutputFile.WriteLine ",,No new hostname listed in input."
    If (Not(IsNull(strDNSDomain))) And (Not(IsEmpty(strDNSDomain))) Then
      intSetDomain = objNicConfig.SetDNSDomain(strDNSDomain)
      If intSetDomain = 0 Then
        objOutputFile.WriteLine ",,Assigned new domain."
      Else
        objOutputFile.WriteLine ",,Unable to assign new domain."
      End If
    Else
        objOutputFile.WriteLine ",,No new DNS domain listed in input."
    End If
    If (Not(IsNull(arrDNSServerSearchOrder(0)))) And _
     (Not(IsEmpty(arrDNSServerSearchOrder(0)))) Then
      intSetDNSServers = objNicConfig.SetDNSServerSearchOrder _
       (strDNSServerSearchOrder)
      If intSetDNSServers = 0 Then
        objOutputFile.WriteLine ",,Assigned new DNS server search order."
      Else
        objOutputFile.WriteLine ",,Unable to assign new DNS server " & _
         "search order."
      End If
    Else
      objOutputFile.WriteLine ",,No new DNS server search order " & _
       "listed in input."
    End If
    If (Not(IsNull(arrDNSDomainSuffixSearchOrder(0)))) And _
     (Not(IsEmpty(arrDNSDomainSuffixSearchOrder(0)))) Then
      intSetDomainSuffixes = objNicConfig.SetDNSSuffixSearchOrder _
       (arrDNSDomainSuffixSearchOrder)
      If intSetDomainSuffixes = 0 Then
        objOutputFile.WriteLine ",,Assigned new DNS domain " & _
         "suffix search order."
      Else
        objOutputFile.WriteLine ",,Unable to assign new DNS domain " & _
         "suffix search order."
      End If
    Else
      objOutputFile.WriteLine ",,No new DNS domain suffix search order " & _
       "listed in input."
    End If
  End If
 
  If (Not(IsNull(blnFullDNSRegistrationEnabled))) And _
   (Not(IsEmpty(blnFullDNSRegistrationEnabled))) Then
    intSetDynamicReg = objNicConfig.SetDynamicDNSRegistration _
     (blnFullDNSRegistrationEnabled, blnDomainDNSRegistrationEnabled)
    If intSetDynamicReg = 0 Then
      objOutputFile.WriteLine ",,Enabled dynamic DNS registration."
    Else
      objOutputFile.WriteLine ",,Unable to enable dynamic DNS registration."
    End If
  Else
    objOutputFile.WriteLine ",,No dynamic DNS registration settings " & _
     "listed in input."
  End If
 
Next
 
End Sub
 
'******************************************************************************
 
Function SetNetbios
 
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
objOutputFile.WriteLine ",,NetBIOS Changes" & VbCrLf
 
For Each objNicConfig in colNicConfigs
  intNetBIOS = objNicConfig.TcpipNetbiosOptions
  If intNetBIOS = intNetbiosTcpip Then
    objOutputFile.WriteLine ",,NetBIOS Over TCI/IP already set to " & _
     "desired value."
    SetNetbios = True
  Else
    objOutputFile.WriteLine ",,Attempting to set NetBIOS Over TCI/IP value."
    intSetNetBIOS = objNicConfig.SetTCPIPNetBIOS(intNetbiosTcpip)
    If intSetNetBIOS = 0 Then
      objOutputFile.WriteLine ",,Successfully set NetBIOS over TCP/IP value."
      SetNetbios = True
    Else
      objOutputFile.WriteLine ",,Unable to set NetBIOS Over TCI/IP value."
      SetNetbios = False
    End If
  End If
Next
 
End Function
 
'******************************************************************************
 
Function SetWins
 
Set objNicConf = objWMIService.Get("Win32_NetworkAdapterConfiguration")
 
objOutputFile.WriteLine VbCrLf & ",,WINS Changes" & VbCrLf & VbCrLf & _
 ",,Attempting to enable WINS ..."
intEnableWINS = objNicConf.EnableWINS(blnDNSEnabledForWINSResolution, _
 blnWINSEnableLMHostsLookup, strWINSHostLookupFile, strWINSScopeID)
If intEnableWINS = 0 Then
  objOutputFile.WriteLine ",,Successfully enabled WINS on all network " & _
   "adapters."
Else
  objOutputFile.WriteLine ",,Unable to enable WINS."
End If
 
objOutputFile.WriteLine ",,Attempting to set WINS primary and " & _
 "secondary servers ..."
intSetWINSServer = objNicConf.SetWINSServer(strWINSPrimaryServer, _
 strWINSSecondaryServer)
If intSetWINSServer = 0 Then
  objOutputFile.WriteLine ",,Successfully set WINS servers."
Else
  objOutputFile.WriteLine ",,Unable to set WINS servers."
End If
 
If (errEnableWINS = 0) And (errSetWINSServer = 0) Then
  SetWins = True
Else
  SetWins = False
End If
 
End Function
 
'******************************************************************************
 
Function GetOsVer
 
Set colOperatingSystems = objWMIService.ExecQuery _
 ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem In colOperatingSystems
  GetOSVer = CSng(Left(objOperatingSystem.Version, 3))
Next
 
End Function




When you use Cscript.exe to run this script, the output is displayed in a comma-delimited text file that is named C:\Scripts\Newclients.csv. You can open a .csv file as a spreadsheet. Output similar to the following is displayed on the monitor when the script runs.

C:\scripts>addclients.vbs

Host: client1

Host: client2

Host: newclient1

Host: oldclient1

Data written to c:\scripts\newclients.csv.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.