Created
February 4, 2018 08:42
-
-
Save Agazoth/3297c0a8ea2e6180c645c102ac1591a9 to your computer and use it in GitHub Desktop.
Gets PSCustomObjects from legacy commands. netstat and arp is allowed so far. Runs on Powershell 5.1 and 6.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-LegacyCommandObject { | |
[CmdletBinding()] | |
param ([ValidatePattern("^(netstat|arp)")] | |
[string]$CommandLine) | |
$Result = iex $CommandLine.split(';',2)[0] | |
$Headers = $Result -match '^\w+(`n`r)*' | |
foreach ($Line in $Result) { | |
if ($Line -match '^$'){$Collect = $False;$Properties=$Null;continue} | |
if ($Headers -contains $Line){$ObjHash=@{}; if ($Line -match ':'){$Parts = $Line.split(':',2).trim();$ObjHash.add($PArts[0],$Parts[1])};$Collect = $true;$CollectParametersHeader=$true;continue} | |
if ($CollectParametersHeader){$Properties = $($Line -split '\s\s+').trim().where{$_ -notmatch '^$'};$CollectParametersHeader=$false;continue} | |
if ($Properties){$Values = $($Line -split '\s\s+').trim().where{$_ -notmatch '^$'};for ($i=0; $i -lt $Properties.count;$i++){$ObjHash[$Properties[$i]]=$Values[$i]}} | |
[PSCustomObject]$ObjHash | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment