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 :
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\/\/