Sunday, 29 April 2012

Show-NetworkInterfaces1.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the NICs in a system and their physical
  4.     address. 
  5. .DESCRIPTION 
  6.     This script is a MSDN Sample recoded in PowerShell. The script
  7.     first gets all the interfaces on the system, then loops through
  8.     them displaying more information about them to the console.
  9.     Note the use of Console.Write in the loop at the end. Not quite
  10.     sure a better PowerShell equivalent other than creating a
  11.     string with all the bytes, then displaying that string. 
  12. .NOTES 
  13.     File Name  : Show-NetworkInterfaces1.ps1 
  14.     Author     : Thomas Lee - tfl@psp.co.uk 
  15.     Requires   : PowerShell Version 2.0 
  16. .LINK 
  17.     This script posted to: 
  18.         http://www.pshscripts.blogspot.com 
  19.     MSDN sample posted to: 
  20.         http://msdn.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx    
  21. .EXAMPLE 
  22.     PSH[c:\foo]> .\Show-Networkinterfaces1.ps1 
  23. Interface information for Cookham8.cookham.net      
  24.   Number of interfaces .................... : 2 
  25. Broadcom NetXtreme 57xx Gigabit Controller 
  26. ========================================== 
  27.   Interface type .......................... : Ethernet 
  28.   Physical address ........................ : 00-1E-4F-95-5C-C4  
  29.  
  30. Microsoft ISATAP Adapter 
  31. ======================== 
  32.   Interface type .......................... : Tunnel 
  33.   Physical address ........................ : 00-00-00-00-00-00-00-E0  
  34.      
  35. #> 
  36.      
  37. # Get computer IP global properties 
  38. $ComputerProperties = [System.Net.NetworkInformation.IpGlobalProperties]::GetIPGlobalProperties() 
  39.  
  40. # Get the nics in this system 
  41. $nics = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() 
  42.  
  43. # Show information header 
  44. "Interface information for {0}.{1}     " -f $ComputerProperties.HostName, $ComputerProperties.DomainName 
  45.   
  46. # Iterate through the NIcs ans outout per nic info 
  47. # First, check if interfaces are found 
  48.  if (!$nics -OR $nics.Length -LT 1) 
  49.     { 
  50.         "  No network interfaces found." 
  51.         return 
  52.     } 
  53.  
  54. # Here print out number of interfaces and interface details 
  55. [System.Console]::WriteLine("  Number of interfaces .................... : {0}" -f $nics.Length) 
  56. Foreach ($adapter in $nics
  57.     { 
  58.         $properties = $adapter.GetIPProperties();    
  59.         " ";"" 
  60.         "{0}" -F $adapter.Description 
  61.         "=" * $adapter.Description.Length 
  62.         "  Interface type .......................... : {0}" -F $adapter.NetworkInterfaceType 
  63.         [System.Console]::Write("  Physical address ........................ : "
  64.         $address = $adapter.GetPhysicalAddress(); 
  65.         $bytes = $address.GetAddressBytes() 
  66.         for($i = 0; $i -lt $bytes.Length; $i++) 
  67.         { 
  68.             # Display the physical address in hexadecimal. 
  69.             [system.Console]::Write("{0}" -f $bytes[$i].ToString("X2")) 
  70.             # Insert a hyphen after each byte, unless we are at the end of the  
  71.             # address. 
  72.             if ($i -NE $bytes.Length -1) 
  73.             { 
  74.                  [System.Console]::Write("-"
  75.             } 
  76.         } 
  77.         [System.Console]::WriteLine() 
  78.     } 

No comments: