Académique Documents
Professionnel Documents
Culture Documents
Windows IT Pro
Windows Exchange Server SharePoint Virtualization Cloud Systems Management Training InfoCenters
HO M E > SYSTEM S M A NA GEM ENT > P O W ERSHELL & SC RI P TI NG > SC RI P TI NG > REP A I R NETW O RK C O NNEC TI O NS FRO M THE C O M M A ND LI NE
Although the article documents the necessary steps, the process isn't in command-line
friendly form. Not only do you need to perform significant editing if you want to copy-
and-paste the solution, but the output can be a bit overwhelming. With the Microsoft
article's documentation as a starting point, I put together the RepairNetwork.cmd script
that Listing 1 shows to automate the network repair process.
RepairNetwork.cmd is ready to run and has cleaner output than the raw IP commands.
However, there's one technical decision in the sequence of commands that some people
might question—the fact that it doesn't explicitly release the DHCP lease before renewal. Windows IT Pro Community
Let's discuss that decision, then look at the script's design. Sign up for Windows IT Pro UPDATE newsletter.
Windows NT 4.0, Windows 9x, and Windows for Workgroups (WFW) systems were all Follow
us
plagued with minor DHCP lease-renewal problems. Symptoms varied in severity,
depending on the specific bug: Clients might fail to get a lease, obtain a lease but retain
invalid option data, or simply experience a long delay in the renewal process. In each
case, the problem was resolved by explicitly releasing DHCP first.
http://windowsitpro.com/scripting/repair-network-connections-command-line 1/4
9/23/2015 Repair Network Connections from the Command Line | Scripting content from Windows IT Pro
The dearth of articles about problems with more modern OSs suggests that, now,
explicitly releasing DHCP first might not be necessary, and that would make sense. Windows Forums
TCP/IP has had a heavy workout on Windows systems over the past few years, and the
core tools are probably bug-free. On my test network, I also discovered that if I explicitly The Windows IT Pro forums
released the IP address first, lease renewal took several seconds longer. That discovery are mov ing to
also makes sense because it's necessary to do a general broadcast to get a new address if my ITforum.com! Get
you don't already know the DHCP server. answers to questions, share
tips, and engage with the IT
I eventually decided that the simplest solution was to include the explicit-release step but professional community .
to comment it out, as you see in callout A in Listing 1. Although I don't anticipate
concerns with new or patched Windows systems, it's always possible that DHCP services
could be provided by a device that has its own problems. Y ou can remove the REM Featured Products
statement from the beginning of the two lines in callout A to explicitly release the current
DHCP lease. Now, let's look at cleaning up the script output.
Advanced OSD with System Center
2012 R2 Configuration Manager
Cleaning Script Output Presented by Johan Arw idm ark
Typically, the first step in cleaning cmd.exe script output is to begin with the @echo off Wednesday, Septem ber 30th
Enroll in the Com plete Course and
statement. This statement tells cmd.exe to not echo the following lines of the script to
Save...
screen (which is cmd.exe's default behavior). The @ symbol at the beginning of a line
prevents that line from echoing, so you don't see the echo off on screen, either. Note that SQL Server Virtualization Deep
Dive
you can flip the echoing back on within a script by using echo on.
Presented by Michael Otey
Thursday, October 22nd
My next step was to clean up the output displayed by the commands. To understand what Enroll in the Com plete Course and
I did and why, we need to talk about what I mean by output. Console applications Save 16%...
The command shell is responsible for displaying information from these streams. It works
by just dumping the information to the screen as it comes in—which brings us to the
problem with our output. The commands we're using dump a lot of information to
standard output. We're just trying to repair the connection quickly, so we don't care about
the details when we succeed, and when we fail, the large quantity of information from
standard output camouflages messages sent back to us by standard error.
The Cmd.exe command shell lets you redirect data from a stream by using the stream
number followed by the redirection operator (>) and the name of the device the data
should be sent to. Devices can be filenames, named output ports on your computer (e.g.,
LPT1:), other streams, or even a generic null device named NUL that simply throws
away everything. So, for example, to throw away the standard output of the command
arp -d *, you would use arp -d * 1> NUL. If you're redirecting standard output, by the
way, you can omit the stream identifier and make this simply arp -d * > NUL. The
command shell assumes that you mean stream 1 if you don't specify a number.
By appending > NUL to most of these commands, we remove most of the irrelevant
information. The standard error stream isn't affected, and errors will still be displayed
onscreen—usually. Some commands don't follow standards, and one of these is the
Nbtstat command. Nbtstat writes at least some of its errors to the standard output
stream. We do want to see Nbtstat errors, so we need to handle suppressing its output a
little differently. Callout B in Listing 1 shows my fix.
Because Nbtstat error messages generally seem to contain the string fail, I can pipe the
output into the find /i "fail" statement. Doing so lets through only lines containing fail.
(The /i option ensures that Find ignores uppercase or lowercase treatment.) Therefore,
failure messages come through. The 1>&2 term on the end of each Nbtstat line redirects
the standard output stream (1) to the standard error stream (2).
Finally, I added some basic useful information back in to the normal display. I could
have simply taken out the @echo off statement and displayed the issued commands;
doing so would also identify the point at which any errors occur. However, I find it more
useful to write a timestamp with the %time% shell variable, followed by a description of
what's happening. The Purging NBT remote cache table statement is generally better for
understanding what's going on than just seeing nbtstat -R echoed. The timestamp also
gives you a feel for how long each step in the process takes. Now, let's take a look at the
http://windowsitpro.com/scripting/repair-network-connections-command-line 2/4
9/23/2015 Repair Network Connections from the Command Line | Scripting content from Windows IT Pro
output and its meaning.
The Output
When you successfully run RepairNetwork.cmd, you'll see output roughly similar to
Figure 1. Y ou can also suppress the normal output but still see errors, thanks to the work
we did on Nbtstat's output. All you need to do is redirect the script's output to NUL
(RepairNetwork > NUL), and any errors will still be displayed.
The script works fine on Windows 2003, XP, and Windows 2000. On Vista, User Account
Control (UAC) introduces some problems. The arp -d, nbtstat -R, and nbtstat -RR
commands all require special privileges to run successfully. To run the script correctly,
either use the Runas command to invoke the script as an administrator or run it within a
command shell session that you've already started with administrative privileges. In
many cases, it might not matter. Most of the networking problems I encounter are
related to IP address lease problems or bad DNS data. The IP renewal and DNS flush both
work fine without the extra privileges.
Tw eet
on Feb 2 7 , 2 0 0 8
arvind (not verif ied)
this artical is v ery usefull in solv ing our day to day problems .
on Feb 2 7 , 2 0 0 8
arvind (not verif ied)
good script
on Sep 2 8 , 2 0 0 8
KeoneBR
on Sep 2 7 , 2 0 0 8
Robert (not verif ied)
Ex ellent Script
on Ja n 9 , 2 0 0 8
sureshgro
Ex cellent Script
on Ja n 9 , 2 0 0 8
AnneG_edit or
Thanks for y our feedback! I'm glad y ou found Alex 's script useful. Just wanted to alert
y ou and other readers to an interv iew with Alex that's coming up in February on
windowsitpro.com. He'll be discussing PowerShell. Stay tuned!
on Feb 1 8 , 2 0 0 8
jeffzhang (not verif ied)
http://windowsitpro.com/scripting/repair-network-connections-command-line 3/4
9/23/2015 Repair Network Connections from the Command Line | Scripting content from Windows IT Pro
GOOD JOB
on Feb 1 3 , 2 0 0 8
amit (not verif ied)
browsin
Re late d Article s
VMware and PowerShell: Managing ESX Server from the Command Line
VMware and PowerShell: Managing ESX Server from the Command Line
Q: I'm trying to pass a bunch of objects from one command to another in PowerShell. The
command complains that it doesn't take pipeline input. How can I pass my list of objects for
further processing?
Q: I'm trying to pass a bunch of objects from one command to another in PowerShell. The
command complains that it doesn't take pipeline input. How can I pass my list of objects for
further processing?
Media Center
Fo llo w Us
RSS
Sitem ap
Site Archiv e
Re late d Site s
Dev Pro SharePoint Pro SQL Serv er Pro SuperSite for Windows IT /Dev Connections m y IT forum
http://windowsitpro.com/scripting/repair-network-connections-command-line 4/4