Skip to content

Instantly share code, notes, and snippets.

@Agazoth
Created February 4, 2018 08:42
Show Gist options
  • Save Agazoth/3297c0a8ea2e6180c645c102ac1591a9 to your computer and use it in GitHub Desktop.
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
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