Whence Function

This type of function should really be built into PowerShell or perhaps get-command should check for reserved words. Get-Command is really, really close to providing whence functionality but it doesn’t cover reserved words (keywords). This function does but it also relies heavily upon Get-Command.  The list of reserved words may be out of date WRT beta 3.  If you spot something that is missing or just plain wrong, let me know (comments are fine).
 
function whence
{
    # As documented in about_Reserved_words plus a few more that
    # aren’t documented in that help topic.
    $keywords =
        "break",   "continue", "do",       "for", 
        "foreach", "while",    "if",       "switch",
        "until",   "where",    "function", "filter",
        "else",    "elseif"    "in",       "return",
        "exit",    "throw",    "trap"

    if ($args.Count -eq 0) {
        ""
        "Usage: whence commandOrKeywordName [commandOrKeywordName]"
        ""
        return
    }

 
    foreach ($name in $args) {
        if ($name -eq $null) {
            "Skipping null argument"
            continue
        }
        elseif ($kwords.Contains($name)) {
            "$name – reserved word"
        }
        else {
            $cmd = get-command $name -ea silentlycontinue
            if ($cmd -eq $null) {
                "$name – unrecognized command"
                continue
            }
            $cmdName = $cmd.Name
            $cmdDef = $cmd.Definition
            # NEED to handle multiple commands matching the name
            switch ($cmd.CommandType) {
                "alias"          { "$cmdName – alias" }
                "function"       { "$cmdName – function" }
                "filter"         { "$cmdName – filter" }
                "script"         { "$cmdName – script – $cmdDef" }
                "externalscript" { "$cmdName – external script : $cmdDef" }
                "application"    { "$cmdName – application : $cmdDef" }
                "cmdlet"         {
                    if ($cmd.Type.FullName -like
                        ‘System.Management.Automation.*’)
                    {
                        "$cmdName – cmdlet (built-in)"
                    }
                    else {
                        "$cmdName – cmdlet (custom) : $cmdDef"
                    }
                }
                default {
                    $type = $cmd.CommandType
                    "$cmdName – unrecognized command type `"$type`""
                }
            }
        }
    }
}

Advertisements
This entry was posted in PowerShell. Bookmark the permalink.

5 Responses to Whence Function

  1. Sung says:

    I have never thought of a functionality of "whence" useful. That is until i ran your "Whence" script.I think it\’d be a good idea if there was a built-in support for it as well.Oh yeah, but wouldn\’t it be better if there was an option to display definitions for alias, function and etc?

  2. Keith says:

    If the script displayed definitions then it becomes not too much different from get-command. Really this whence function could be completely replaced by get-command if get-command reported on reserved words.

  3. Unknown says:

    hello,keith: I tested your script.It\’s work nicely but I found something relative to security question: If your have a test.txt in your "f:\\test2" directory.when I tap the following command to test your function,the "whence" will execute it rather than examine the argument.I think it\’s dangerous.##################################MSH F:\\test> set-location f:\\test2MSH F:\\test2> get-childitem Directory: Microsoft.Management.Automation.Core\\FileSystem::F:\\test2Mode LastWriteTime Length Name—- ————- —— —–a— 2006-1-23 14:39 0 test.txtMSH F:\\test2> whence $(remove-item test.txt)get-command : The argument cannot be null or empty.At line:12 char:31+ $cmd = get-command <<<< $name -ea silentlycontinue – unrecognized commandMSH F:\\test2> get-childitemMSH F:\\test2>################################# I think first excluding the args\’ parameter is better.################################## I used ask the reservord word "throw" in the newgroup,and you had reply my question. Thanks again.(which one there you don\’t included in your list) greeting!

  4. Keith says:

    Thanks for pointing out those problems. I fixed the exception you are seeing and added the "throw" keyword. WRT the security exception I\’m not sure there\’s anything a script can do in this case. When you execute "whence $(some command)" in MSH, MSH evaluates the "$(some command)" first and then passes the result into whence as the first argument. In the case of remove-item, this results in $null being sent to whence. Now I skip null arguments.

  5. Unknown says:

    If "global" is keyword,why not the"script" , "local" and "private"?

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