Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

The Easy Way to Monitor for an IP Address by Using PowerShell

$
0
0

Summary: The Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to monitor for acquiring an IP address.

Hey, Scripting Guy! Question Hey, Scripting Guy! We have a problem. It seems that when people with laptops come into the office, it takes forever for them to obtain network access. On the tool bar, a blue circle of death spins in an infinite loop. When I hover over the circle, it says “identifying network.” There is nothing to identify—it is our corporate network. I recently figured out that the circle is lying to me. It is not really trying to identify the network; but rather, it is waiting on an Internet Protocol (IP) address. I know you can use a ping command to ping forever, but I can never remember the syntax. In addition, it does not really tell me what I want to know. What I really want to know is if the computer has obtained an IP address. Can I use Windows PowerShell to do this?

—MH

Hey, Scripting Guy! Answer Hello MH,

Microsoft Scripting Guy, Ed Wilson, is here. Often when one thinks about monitoring, one turns ones attention to Windows Management Instrumentation (WMI) events. In fact, I recently published Insider’s Guide to Using WMI Events and PowerShell that listed Hey, Scripting Guy! resources for working with this powerful and cool technology. But at times, such an approach is a bit like using a steamroller to make hamburger patties—it might work, but is not necessarily the easiest way to do things.  

MH, one easy way to monitor for acquisition of an Internet Protocol (IP) address is to use the range operator and pair it with the ForEach-Object cmdlet. In fact, this technique is one of my top ten favorite Windows PowerShell tricks because it is so flexible and so powerful. My approach here is to do something really easy, really quick, and with minimal typing. My approach is not efficient, elegant, or even “correct” (as far as Windows PowerShell purists go). I would venture there are even easier ways to do this. But the advantage here is that the command is easy to understand, easy to remember, and easy to type.

The command that follows begins by using the range operator to create an array of numbers from 1 through 500. These numbers cross the pipeline one at a time. The ForEach-Object cmdlet calls the ipconfig command once for each number. The results of ipconfig pipe to the Select-String cmdlet. Select-String displays only the line of output that contains the letters ipv4. By default, there is no alias for the Select-String cmdlet, but remember that by using Tab Expansion, you can greatly reduce your typing load. The following command illustrates how to use Select-String to retrieve only the line of text containing ipv4.

PS C:\> ipconfig | Select-String ipv4

   IPv4 Address. . . . . . . . . . . : 192.168.0.54

OK MH, so I can now find my Internet Protocol (IP) address from ipconfig by using the Select-String cmdlet. The next thing, I need to do is to wait for a couple of seconds and clear the Windows PowerShell console host. To do this, I use the Start-Sleep cmdlet (sleep is an alias) and cls (an alias for the Clear-Host function). The complete command is shown here.

1..500 | % {ipconfig | Select-String ipv4 ; sleep 2; cls }

When I run the command, for the first two seconds it displays both the command and the output of the command as shown here.

Image of command output

After the first two seconds of run time, the Windows PowerShell console clears, and only the IPv4 Address displays as shown here.

Image of command output

MH, that is all there is to using Windows PowerShell to monitor for changes in the IP address. Join me tomorrow for more Windows PowerShell cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>