{"id":8991,"date":"2012-06-20T00:01:00","date_gmt":"2012-06-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/06\/20\/the-easy-way-to-monitor-for-an-ip-address-by-using-powershell\/"},"modified":"2012-06-20T00:01:00","modified_gmt":"2012-06-20T00:01:00","slug":"the-easy-way-to-monitor-for-an-ip-address-by-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/the-easy-way-to-monitor-for-an-ip-address-by-using-powershell\/","title":{"rendered":"The Easy Way to Monitor for an IP Address by Using PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: The Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to monitor for acquiring an IP address.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;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 &ldquo;identifying network.&rdquo; There is nothing to identify&mdash;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 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ping\" target=\"_blank\">ping<\/a> 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?<\/p>\n<p>&mdash;MH<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello MH,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Often when one thinks about monitoring, one turns ones attention to <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394582(VS.85).aspx\" target=\"_blank\">Windows Management Instrumentation<\/a> (WMI) events. In fact, I recently published <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/an-insider-s-guide-to-using-wmi-events-and-powershell\/\" target=\"_blank\">Insider&rsquo;s Guide to Using WMI Events and PowerShell<\/a><i> <\/i>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 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Steamroller\" target=\"_blank\">steamroller<\/a> to make hamburger patties&mdash;it might work, but is not necessarily the easiest way to do things. &nbsp;<\/p>\n<p>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 <b>ForEach-Object<\/b> cmdlet. In fact, this technique is one of my <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=top%20ten%20tricks\" target=\"_blank\">top ten favorite Windows PowerShell tricks<\/a> 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 &ldquo;correct&rdquo; (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.<\/p>\n<p>The command that follows begins by using the range operator to create an array of numbers from 1 through 500. These numbers cross the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\" target=\"_blank\">pipeline<\/a> one at a time. The <b>ForEach-Object<\/b> cmdlet calls the <b>ipconfig<\/b> command once for each number. The results of <b>ipconfig<\/b> pipe to the <b>Select-String<\/b> cmdlet. <b>Select-String<\/b> displays only the line of output that contains the letters <b>ipv4<\/b>. By default, there is no alias for the <b>Select-String<\/b> cmdlet, but remember that by using Tab Expansion, you can greatly reduce your typing load. The following command illustrates how to use <b>Select-String<\/b> to retrieve only the line of text containing <b>ipv4<\/b>.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; ipconfig | <b>Select-String<\/b> ipv4<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; IPv4 Address. . . . . . . . . . . : 192.168.0.54<\/p>\n<p>OK MH, so I can now find my Internet Protocol (IP) address from <b>ipconfig<\/b> by using the <b>Select-String<\/b> 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 <b>Start-Sleep<\/b> cmdlet (<b>sleep<\/b> is an alias) and <b>cls<\/b> (an alias for the <b>Clear-Host<\/b> function). The complete command is shown here.<\/p>\n<p style=\"padding-left: 30px\">1..500 | % {ipconfig | <b>Select-String<\/b> ipv4 ; sleep 2; cls }<\/p>\n<p>When I run the command, for the first two seconds it displays both the command and the output of the command as shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8875.hsg-6-20-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8875.hsg-6-20-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>After the first two seconds of run time, the Windows PowerShell console clears, and only the IPv4 Address displays as shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2746.hsg-6-20-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2746.hsg-6-20-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>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.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\" target=\"_blank\">Official Scripting Guys Forum.<\/a>&nbsp;See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: The Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to monitor for acquiring an IP address. &nbsp;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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[36,16,42,340,37,3,4,134,45],"class_list":["post-8991","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-management","tag-desktop-management","tag-events-and-monitoring","tag-general-mgmt-tasks","tag-networking","tag-scripting-guy","tag-scripting-techniques","tag-troubleshooting","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: The Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to monitor for acquiring an IP address. &nbsp;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=8991"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8991\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=8991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}