Inspect Methods in PowerShell


*EDIT: Code updated (Thanks Rob)

So the more you use PowerShell the less you want to leave it. When you end up with an object that you have no clue how to use you’ll likely use Get-Member to inspect it. Once you see what members it has and find a method you’d like to use you need to figure out what the params are.

$str = “hello”

$str | get-member

We can see that string has a bunch of methods and perhaps we want to use the compare but don’t know what the different overloads are so we can take a peek at them

$str.compare

You’ll notice there is () on that method call, and that’s what triggers PowerShell to show the different ways to use that method.

One problem I have with this is that some of them are so long you can’t see what belongs to what so here is my first attempt at showing it cleanly.

[string]::Compare.OverloadDefinitions -replace ‘\(‘,”`n`t ” -replace ‘,|\)’,”`n`t”

Thanks Rob for the shorter code.

Whats next? A function to do this.

function Format-Definition

{

[CmdletBinding()]

Param

(

[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]

[System.Management.Automation.PSMethod] $method

)

$defs = $method.OverloadDefinitions

foreach($def in $defs)

{

if($def -like “*()”)

{Write-Host $def -f 6}

else

{

$splits = $def -split ‘\(|\)|,’

write-host $splits[0] -f 6

$splits[1..100] | %{$rtn = $_.trim() -split ” ”

Write-host “`t$($rtn[0]) ” -F 8 -NoNewline

write-host $rtn[1]

}}}}

$str.compare | format-definition

Much better! Well, except for the colors, blame Rob for that :)

I don’t use the default color scheme so I figured I’d let you change that yourselves.

Advertisements

About jrich

I am the Solutions Architect for Apex Learning in Seattle WA. I've been working with computers since I was 13. Started programming when I was 14. Had my first IT job as tech support at an ISP at the age of 15 and became a network admin at the age of 17. Since then I've worked at a variety of small to mid size companies supporting, maintaining and developing all aspects of IT. Mostly working with Windows based networks but have recently been working with Solaris system as well. I created this blog mostly as a place for me to take my own notes, but also share things that I had a hard time finding the info for.

Posted on April 20, 2012, in WMF (Powershell/WinRM) and tagged , , , . Bookmark the permalink. 8 Comments.

  1. [string]::Compare.OverloadDefinitions -replace ‘\(‘,”`n ” -replace ‘,|\)’,”`n “

  2. The posting editor is messing with that. There should be 4 spaces after the first `n, and 3 after the second one.

  3. Or use a tab and one space on the first one , and just a tab on the second.

    [string]::Compare.OverloadDefinitions -replace ‘\(‘,”`n`t ” -replace ‘,|\)’,”`n`t”

  4. while you’re at it why not whip out some color coding? :)

  5. You sure you want that? You’re talking to someone who liked “Hotdog Stand” (I’m color blind) :P

  6. [string]::Compare.OverloadDefinitions -replace ‘\(‘,”`n`t ” -replace ‘,|\)’,”`n`t” -split(“`n”) |
    %{
    switch -regex ($_) {
    ^\S {Write-Host $_ -F 6}
    ^\s+[^\.]+$ {Write-host $_ -F 11}
    Default {Write-host $_ -F 13}
    }
    }

    • was thinking coloring types and names, so this line
      ^\s+[^\.]+$ {Write-host $_ -F 11}
      would be split on space and the type would be one color and the name another

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: