none
Explanation for about_splatting example? RRS feed

  • Question

  • Hi,

    I was just exploring the help files in Powershell and stumbled upon the about_splatting examples. There is one example that looks like this:

    function Get-MyCommand 
            { 
                Param ([switch]$P, [switch]$C)
                if ($P) { Get-Process @Args }
                if ($C) { Get-Command @Args }
            }
    PS C:\> Get-MyCommand -P -C -Name PowerShell

    The results are quite interesting. If I run:

    Get-Process -Name PowerShell

    and then

    Get-Command Powershell

    ... I get very different results compared to if I run them at the same time, like this:

    Get-Process -Name PowerShell
    Get-Command -Name Powershell

    I guess this was discussed during the scripting games 2012 (Rohn Edwards, respect!), but I wasnt around at that time and I cant find anything related if I search for it.

    The last part of the result looks similar to the result from this:

    Get-Command -Name PowerShell | select *
    Can someone please explain how this works? Why do I get two very different results if I run the commands separately compared to if i run them after one another, on separate lines, in a script?

    Thursday, February 12, 2015 8:49 PM

Answers

  • That is a completely different question.

    The formatter detect the object type in the pipeline and formats when it is output.  You have output one object type but the formatter doesn't understand object #2 so it just outputs it raw.  This is why you should only ever pass back one object type unless you want the outcome.  TO override you can use a formatter on each command


    ¯\_(ツ)_/¯

    • Marked as answer by John-R Thursday, February 12, 2015 10:42 PM
    Thursday, February 12, 2015 10:29 PM

All replies

  • First run this and look closely at the output.  It is not what you think.

    function Get-MyCommand { 
        Param (
            [switch]$P, 
            [switch]$C
        )
       'ARG:0'+$args[0]
       'ARG1:'+$args[1]
                 #if ($P) { Get-Process @Args }
                 #if ($C) { Get-Command @Args }
     }
    Get-MyCommand -P -C -Name PowerShell
    


    ¯\_(ツ)_/¯

    Thursday, February 12, 2015 9:35 PM
  • Now look closely at the output this time:

    function Get-MyCommand { 
        Param (
            [switch]$P, 
            [switch]$C
        )
        $PSBoundParameters
                 
        #if ($P) { Get-Process @Args }         
        #if ($C) { Get-Command @Args }
     }
    Get-MyCommand -P -C -Name PowerShell


    ¯\_(ツ)_/¯

    Thursday, February 12, 2015 9:37 PM
  • Now consider this:

    function Get-MyCommand { 
           Get-Process @args
    }
    Get-MyCommand PowerShell


    ¯\_(ツ)_/¯

    Thursday, February 12, 2015 9:39 PM
  • Thanks for taking the time and thanks for the explanations.

    What I still dont understand is why these two commands, even if they are separated:

    Get-Process -Name PowerShell; Get-Command -Name Powershell
    ... seems to make Get-Command output all properties instead of the standard output with just a few properties.

    It behaves almost as if I would have written this:

    Get-Process -Name PowerShell
    Get-Command -Name Powershell | select *

    Thursday, February 12, 2015 10:02 PM
  • That is a completely different question.

    The formatter detect the object type in the pipeline and formats when it is output.  You have output one object type but the formatter doesn't understand object #2 so it just outputs it raw.  This is why you should only ever pass back one object type unless you want the outcome.  TO override you can use a formatter on each command


    ¯\_(ツ)_/¯

    • Marked as answer by John-R Thursday, February 12, 2015 10:42 PM
    Thursday, February 12, 2015 10:29 PM