Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Script to retrieve network details using ipconfig
  2.  
  3. # Interface Alias
  4. $interface = get-wmiobject win32_networkadapter | select netconnectionid, netconnectionstatus | Where-Object {$_.netconnectionstatus -match 2}
  5. $interface = $interface.netconnectionid;
  6.  
  7. <# This function takes variable as a parameter and returns a string.
  8. Desc: "ipconfig"" command returns string as a result.  
  9. like for IPv6, "Link-local IPv6 Address . . . . . : fe80::18fd:e072:774e:6799%11".
  10. we are replacing the first colon ":"  to "," (in order to avoid conflicts with
  11. other colon used in the value i.e fe80::18fd:e072:774e:6799%11 )
  12. so that we can group rest of the string as a result after comma "," #>
  13.  
  14. function getResult($elem) {
  15.     $replacedStr = $elem -replace '^(.*?):(.*)','$1,$2';
  16.     $result = $replacedStr.ToString().Split(',')[1]
  17.     return $result;
  18. }
  19.  
  20.  
  21. #IPV4
  22. $ipsv4 = ipconfig /all | where-object {$_ –match 'IPv4 Address'}
  23.  
  24. $ipv4 = @();
  25. ForEach( $ip in $ipsv4 ) {
  26.     $ipv4Res = getResult($ip);
  27.     if ( !$ipv4Res ) {
  28.         continue;
  29.     }
  30.      $ipv4 += $ipv4Res
  31. }
  32.  
  33.  
  34. #DNS Server
  35. $dnsServers = ipconfig /all | where-object {$_ –match 'DNS Servers'}
  36.  
  37. $dns = @();
  38. ForEach( $dnsServer in $dnsServers ) {
  39.      $dnsRes = getResult($dnsServer);
  40.      if( !$dnsRes ) {
  41.         continue;
  42.      }
  43.      $dns += $dnsRes;
  44. }
  45.  
  46. #Gateway
  47. $gateways = ipconfig /all | where-object {$_ –match 'Default Gateway'}
  48.  
  49. $gateway = @();
  50. ForEach( $gw in $gateways ) {
  51.      $gatewayRes = getResult($gw);
  52.      if( !$gatewayRes ) {
  53.         continue;
  54.      }
  55.      $gateway += $gatewayRes;
  56. }
  57.  
  58. $obj = New-Object System.Object ;
  59. $obj | Add-Member -type NoteProperty -Name IPV4 -Value $ipv4;
  60. $obj | Add-Member -type NoteProperty -Name DNSServers -Value $dns;
  61. $obj | Add-Member -type NoteProperty -Name DefaultGateway -Value $gateway;
  62. $obj | Add-Member -type NoteProperty -Name Interface -Value $interface;
  63. $obj | Format-List;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement