Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Script to retrieve network details using ipconfig
- # Interface Alias
- $interface = get-wmiobject win32_networkadapter | select netconnectionid, netconnectionstatus | Where-Object {$_.netconnectionstatus -match 2}
- $interface = $interface.netconnectionid;
- <# This function takes variable as a parameter and returns a string.
- Desc: "ipconfig"" command returns string as a result.
- like for IPv6, "Link-local IPv6 Address . . . . . : fe80::18fd:e072:774e:6799%11".
- we are replacing the first colon ":" to "," (in order to avoid conflicts with
- other colon used in the value i.e fe80::18fd:e072:774e:6799%11 )
- so that we can group rest of the string as a result after comma "," #>
- function getResult($elem) {
- $replacedStr = $elem -replace '^(.*?):(.*)','$1,$2';
- $result = $replacedStr.ToString().Split(',')[1]
- return $result;
- }
- #IPV4
- $ipsv4 = ipconfig /all | where-object {$_ –match 'IPv4 Address'}
- $ipv4 = @();
- ForEach( $ip in $ipsv4 ) {
- $ipv4Res = getResult($ip);
- if ( !$ipv4Res ) {
- continue;
- }
- $ipv4 += $ipv4Res
- }
- #DNS Server
- $dnsServers = ipconfig /all | where-object {$_ –match 'DNS Servers'}
- $dns = @();
- ForEach( $dnsServer in $dnsServers ) {
- $dnsRes = getResult($dnsServer);
- if( !$dnsRes ) {
- continue;
- }
- $dns += $dnsRes;
- }
- #Gateway
- $gateways = ipconfig /all | where-object {$_ –match 'Default Gateway'}
- $gateway = @();
- ForEach( $gw in $gateways ) {
- $gatewayRes = getResult($gw);
- if( !$gatewayRes ) {
- continue;
- }
- $gateway += $gatewayRes;
- }
- $obj = New-Object System.Object ;
- $obj | Add-Member -type NoteProperty -Name IPV4 -Value $ipv4;
- $obj | Add-Member -type NoteProperty -Name DNSServers -Value $dns;
- $obj | Add-Member -type NoteProperty -Name DefaultGateway -Value $gateway;
- $obj | Add-Member -type NoteProperty -Name Interface -Value $interface;
- $obj | Format-List;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement