Part 9: Scenario for Adding New Clients
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
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
Define constants for use with the FileSystemObject.
Create variables to hold the paths of the input and output files and counters for the final tally.
Call the GetInput function, passing it the path to the input file.
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.
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.
Write a header for the output file containing the date and the number of computers in the input file.
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.
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.
Read the first element of the new array, the host name, into a variable and display it.
Call the PingClient function, passing it the host name.
If the ping succeeds, connect to the WMI service on the host.
If no error occurs in connecting, assign the remaining 21 elements of the new array to variables representing the settings for this host.
Write a heading for this host to the text file.
Call the GetSettings subroutine to retrieve the current settings of this host and write them to the output file.
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.
If the network client is to be removed, call the RemoveClient subroutine.
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.
Check the parameter for DNS to determine if DNS settings are to be changed. If so, call the SetDNS subroutine.
Check the parameter for WINS to determine if DNS settings are to be changed. If so, first call the SetNetBIOS function.
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.
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.
If the script could not connect to WMI on this host, write an error message to the output file.
If the ping to this host failed, write an error message to the output file.
Write a footer to the output file summarizing the number of clients added, removed, and updated and any errors; and close the output file.
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.
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.
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.
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.
The SetDHCP subroutine calls the EnableDHCP method of Win32_NetworkAdapterConfiguration to enable DHCP on each IP-enabled network adapter.
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.
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.
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.
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.
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 |
|
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.