Wednesday, 6 October 2010

Get-LoopBack.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script checks whether a parameter is a Loopback Address  
  4. .DESCRIPTION 
  5.     This script checks to see if the passsed string is an IPV4
  6.     or an IPv6 loopback address and if so, displays details.
  7. .NOTES 
  8.     File Name  : Get-LoopBack.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com  
  14.     MSDN sample posted to: 
  15.         http://msdn.microsoft.com/en-us/library/system.net.ipaddress.isloopback.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-LoopBack.ps1 
  18.     Your input address: \127.0.0.1\ is an IPv4 loopback address whose internal format is: 127.0.0.1. 
  19. .EXAMPLE 
  20.     PSH [C:\foo]: .\Get-LoopBack.ps1 ::1 
  21.     Your input address: \::1\ is an IPv6 loopback address whose internal format is: ::1. 
  22. .EXAMPLE 
  23.     PSH [C:\foo]: .\Get-LoopBack.ps1 131.107.2.200 
  24.     Your input address: \131.107.2.200\ is not a loopback address. 
  25. .PARAM 
  26.     $IPAddress - Address to look up to see if it's Loopback 
  27. #> 
  28. param
  29. [String] $IpAddress = "127.0.0.1" 
  30. )  
  31. # Setup Default answer! 
  32. $loopBack=" is not a loopback address."
  33.  
  34. # Perform syntax check by parsing the address string entered by the user. 
  35. $Address = [System.Net.IPAddress]::Parse($IpAddress); 
  36.  
  37. # Perform semantic check by verifying that the address is a valid IPv4  
  38. # or IPv6 loopback address.  
  39. if([System.Net.IPAddress]::IsLoopback($Address) -and ($address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6) ) { 
  40.         $loopBack" is an IPv6 loopback address "
  41.                     "whose internal format is: " + $Address.ToString() + "."
  42. }            

No comments: