1

I want to address the table-formated output of my function like an object, i.e. the row [2] column 'Action' should be addressable like $r[2].Action. How can I do that?

I tried using 'ConvertFrom-SourceTable' from the powershellgallery, but that didn't work. And I'd rather have a simple solution without extra installs.

Function Get-NetFirewallRulesByPort {

    Param ( [Parameter(Mandatory=$true)]
            [ValidateRange(1,65535)]
            [int] $port )

    Get-NetFirewallRule | 
        where { ($_ | Get-NetFirewallPortFilter).LocalPort -eq ${port} } |
        select Enabled, Action, Direction, DisplayName | ft -AutoSize

}

# usage:
$r = (Get-NetFirewallRulesByPort 443)
$r

# required:
Write-Host $r[2].Action
Write-Host $r[1].DisplayName
# etc. ...

1 Answer 1

5

Because of this | ft -AutoSize in your function, you force it to output the gathered data formatted for display purposes only

Trying to decode that later to make it an array of objects again is definitively not the way to go.

Remove | ft -AutoSize from the function so your variable $r will receive a set of objects

If you must, display the result using that $r variable in several ways:

$r | Format-Table -AutoSize

or

$r | Out-GridView

By keeping it as objects, you can now do what you want, like $r[2].Action (provided you have at least 3 objects returned of course..)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.