Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell Get-Ipconfig function

 

In a comment on this post PowerShell on joeware.net , I translated a Perl example that used ipconfig information, I used the same regex techniques as in the Perl example, but this made me think of a more PoSHy  general wrapper that exposes the ipconfig information as an object in PowerShell.

for convenience I also added text output to the console by default (you can suppress this with the -Silent Switch) so you can use it as a quick lookup as Ipconfig, but it also returns a dynamically build Object that you can use to access all information hierarchal by digging into the properties.

You can use the properties (and tab completion) to access the property of interest from the output for interactive or script usage.

See examples below :

image

The Get-IpConfig function shown above looks like this :

Function get-Ipconfig ([switch]$silent){
  $ipc = ipconfig /all
  $ipcObject = New-Object object
  $ipc |% {

    if (.{$m = [regex]::Matches($_,'^(\w.*?):*$');$m[0]}) {
        $script:o = New-Object object
        Add-Member -InputObject $o -MemberType ScriptMethod -Name ToString -Value {$this.psobject.properties |% {$_.name + " = " + $_.value + "`n"}} -force
        Add-Member -InputObject $ipcObject -MemberType NoteProperty -Name $m[0].groups[1].value.replace(' ','') -Value $o
    } elseIf (.{$m = [regex]::Matches($_,'(.*?)[ \.]*: (.*)');$m[0]}){
        $script:p = $m[0].groups[1].value.replace(' ','')
        Add-Member -InputObject $script:o -MemberType NoteProperty -Name $m[0].groups[1].value.replace(' ','') -Value $m[0].groups[2].value
    } elseIf (.{$m = [regex]::Matches($_,'\w+.*');$m[0]}){
        $script:o."$p" += ";" + $m[0].groups[0] 
    }
  }
  if (!$silent) {$ipcobject | fl | out-string | write-host -fore 'Yellow'}
  $ipcobject
}

Note that this is only a simple example and as I build the property list dynamic I do keep all properties text, and not translate them but the use of nested Noteproperties might be interesting to look at, as it might come of use sometime, note also the overload of the ToString() Method for custom formatting with Format-List ;-)

Enjoy,

Greetings /\/\o\/\/

Published Monday, June 30, 2008 4:10 PM by MoW

Comments

# re: PowerShell Get-Ipconfig function

I'm still relatively new to PowerShell, and I was wondering about this line:

"    if (.{$m = [regex]::Matches("...

What does the dot after the open parens do?

Monday, June 30, 2008 6:26 PM by tojo2000

# re: PowerShell Get-Ipconfig function

@ tojo2000

Good catch,

This is because I use a ; in my IF condition (it are basicly 2 lines),

but ; signs are not permitted between () as powershell reads ; as end of line it will complain about a missing closing ;

I solve this by using a scriptblock {} instead that I invoke in the current scope by using the DotSource operator .

This will give an error :

elseIf ($m = [regex]::Matches($_,'\w+.*');$m[0]){

this way it works

elseIf (.{$m = [regex]::Matches($_,'\w+.*');$m[0]}){

This workaround is only needed when you use a ; in the IF condition

h.t.h.

enjoy,

Greetings /\/\o\/\/

Tuesday, July 01, 2008 9:55 AM by MoW

# re: PowerShell Get-Ipconfig function

Shouldn't this be done using .net classes:

http://www.codeproject.com/KB/IP/IpXconfig.aspx

and allow the cmdlet to also change IP settings?

Tuesday, July 01, 2008 8:03 PM by geokes

# re: PowerShell Get-Ipconfig function

Awesome, thanks.

Wednesday, July 02, 2008 6:37 AM by tojo2000

# re: PowerShell Get-Ipconfig function

Hi. I have a comment/question: On Vista, I get Properties like unneladapterLocalAreaConnection*11. Naturally, I could fix the script myself as this property name is 'clumsy' as it needs to be surrounded by quotes, but PowerTab should have done that for me?! This leads me to the ultimate question: When will we see a new version of PowerTab? I'm using 0.99 beta 2 and are having some 'issues' ;)

Best regards

Per

Friday, July 04, 2008 5:32 AM by perost

# re: PowerShell Get-Ipconfig function

@ geokes,

as this was more an exercize in workng with natic apps this way

@ perost,

Yes I tryed to avoid that by removing spaces from the names did not encounter that myself,

also i noted this was a quick exercize and you can better add properies manualy with decent types.

about Powertab, if your on CTP2 watch that space ;-)

Saturday, July 05, 2008 1:18 PM by MoW

# re: PowerShell Get-Ipconfig function

I find it simply amazing that  Powershell can use the .NET  to find the Broadcast and Loopback with little complication, but not the host IP Address:

[System.Net.IPAddress]::Broadcast.IPAddressToString

or

[System.Net.IPAddress]::Loopback.IPAddressToString

I can find all NetworkInterface information BUT the IP easily enough:

[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()

But if I want my local host IP address through .NET I need some kludge like:

$host_name = [System.Net.Dns]::GetHostName() ; [System.Net.Dns]::Resolve("$host_name")

Or I can dredge up some other not quite satisfactory kludge from gwmi:

$NetworkInfo =gwmi -query "SELECT * FROM Win32_NetworkAdapterConfiguration"

function NetworkInfoSort {$NetworkInfo | Select-Object IPAddress,Description,Index,DefaultIPGateway | sort-object Index}

NetworkInfoSort

or

gwmi -class win32_NetworkAdapterConfiguration | %{ $_.IPAddress }

or

gwmi -query "SELECT IPAddress FROM Win32_NetworkAdapterConfiguration" | Select IPAddress

I wish I could just do something like:

[System.Net.IPAddress]::LocalHost.IPAddressToString

or

[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces.IPAddress

or

Get-ipconfig

Monday, July 07, 2008 1:04 AM by rferrisx

# re: PowerShell Get-Ipconfig function

@rferrisx

I got it working like this :

[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() |% {$_.getIPProperties().UnicastAddresses[0]}

but there are some issues working with objects returned, when you return some properties, this is a bug I tested this on V@ and it is still there

.

Greetings /\/\o\/\/

Monday, July 07, 2008 11:26 AM by MoW

# re: PowerShell Get-Ipconfig function

Wow...I am not sure how you came to that, but thanks. Adding something like:

| Select Address, *Lifetime | ft -auto

gives useful information on the leases per IP

Something like this also works but gets a little weird when you source the objects:

$hostnameX = [System.Net.Dns]::Resolve([System.Net.Dns]::GetHostName

Monday, July 07, 2008 1:18 PM by rferrisx

# Bookmarks about Powershell

Sunday, July 20, 2008 12:15 PM by Bookmarks about Powershell

# re: PowerShell Get-Ipconfig function

MoW (and tojo2000),

Why not use $($m = [regex]::Matches($_,'^(\w.*?):*$');$m[0]) instead of dot sourcing a script block?  Is the latter faster?

Friday, August 01, 2008 12:54 PM by tomtrias

# re: PowerShell Get-Ipconfig function

@tomtrias,

Just not tought about that, your way actualy might be better

Greetings /\/\o\/\/

Friday, August 01, 2008 1:37 PM by MoW

# re: PowerShell Get-Ipconfig function

MoW,

Fair enough; I'm reading through Bruce Payette's "PowerShell in Action", otherwise I wouldn't even know about the $() syntax.  Before that, I pretty much relied on your blog for my PowerShell documentation. ;-)

Friday, August 01, 2008 6:00 PM by tomtrias
Anonymous comments are disabled