Getting ALL Your Parameters

Getting ALL Your Parameters

  • Comments 5

When you write a function, you can name a set of parameters and PowerShell will bind command line arguments to them. Any command line argument that is NOT bound do a parameter is available as $args. Let me illustrate:

PS>function test ($a) {"Remaing args = $args"}
PS>test -A value
Remaing args =
PS>
PS>test -A value -B value2 -C this is a test
Remaing args = -B value2 -C this is a test
PS>
PS>function test ($a,$b,$c) {"Remaing args = $args"}
PS>test -A value -B value2 -C this is a test
Remaing args = is a test

Eric asked me the question, "How can I get ALL the arguments and not just the "remaining arguments". You can get this but it requires a tiny amount of work on your part. If you haven't already explored $MYINVOCATION – you should. This variable tells you about how your function was invoked. It is awesome! One of its properties is LINE which provides the "line" that was submitted which caused your function to get invoked. Again, an example makes this clear:

PS>function test {$myinvocation}
PS>test -A Value -b Value2 -c Value3 this is a test


MyCommand : test
ScriptLineNumber : 1
OffsetInLine : 5
ScriptName :
Line : test -A Value -b Value2 -c Value3 this is a test
PositionMessage :
At line:1 char:5
+ test <<<< -A Value -b Value2 -c Value3 this is a test
InvocationName : test
PipelineLength : 1
PipelinePosition : 1

PS>write-host "test";test -A Value -b Value2 -c Value3 this is a test
test


MyCommand : test
ScriptLineNumber : 1
OffsetInLine : 23
ScriptName :
Line : write-host "test";test -A Value -b Value2 -c Value3 this is a test
PositionMessage :
At line:1 char:23
+ write-host "test";test <<<< -A Value -b Value2 -c Value3 this is a test
InvocationName : test
PipelineLength : 1
PipelinePosition : 1

From there you have the building blocks to get your answer. The OffsetInLine tells you where the args start:

PS>function test {$myinvocation.line.substring($myInvocation.OffSetInLine)}
PS>write-host "PowerShell";test -A Value -b Value2 -c Value3 this is a test
PowerShell
-A Value -b Value2 -c Value3 this is a test


This isn't a perfect solution because it doesn't address other things on the line:


PS>write-host "PowerShell";test -A Value -b Value2 -c Value3 this is a test; write-Host "Rocks"
PowerShell
-A Value -b Value2 -c Value3 this is a test; write-Host "Rocks"
Rocks

Enjoy!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

 

Leave a Comment
  • Please add 7 and 7 and type the answer here:
  • Post
  • PingBack from http://testsubdomain.netmoviehost.com/getting-all-your-parameters/

  • hi,my offsetinline is -2147483648,why and how to solve it:)

    PS C:\> function test {$myinvocation}

    PS C:\> test -A Value -b Value2 -c Value3 this is a test

    MyCommand        : test

    ScriptLineNumber : 1

    OffsetInLine     : -2147483648

  • in MAC OS X to get the list of items in your directory you write this "ls -la" I was wondering what is the equivalent parameter in windows XP sorry I'm a new ms user, I would appreciate any help from you guys thanks

  • How do you get the whole command line if line continuation is involved (either due to backtick at end of line or due to syntactically incomplete line)? In this case $myinvocation.line only contains the first line

  • The Line field gets populated only when the powershell script is run with its parameters, whereas when this gets run from a batch file (.cmd), then the line information is not existent.

    Eg: Test.cmd - Invokes the powershell script

    <

    @echo off

    powershell -ExecutionPolicy Unrestricted -File "%~dp0\Test.ps1" %1 %2

    >

    Test.ps1

    <

    Param (

           [switch]$Verbose,

           [string]$LogFilePath=( Join-Path $env:TMP "test.log" )

       )

    $MyInvocation

    >

    ::Output of the script when "Test.cmd -Verbose" is run :: Line information is not present ::

    MyCommand             : Test.ps1

    BoundParameters       : {[Verbose, True]}

    UnboundArguments      : {}

    ScriptLineNumber      : 0

    OffsetInLine          : 0

    HistoryId             : 1

    ScriptName            :

    Line                  :

    PositionMessage       :

    PSScriptRoot          :

    PSCommandPath         :

    InvocationName        : C:\temp\test.ps1

    PipelineLength        : 2

    PipelinePosition      : 1

    ExpectingInput        : False

    CommandOrigin         : Runspace

    DisplayScriptPosition :

    ::Output of the script when "Test.ps1 -Verbose" is run :: Line information is present ::

    MyCommand             : Test.ps1

    BoundParameters       : {[Verbose, True]}

    UnboundArguments      : {}

    ScriptLineNumber      : 1

    OffsetInLine          : 1

    HistoryId             : 19

    ScriptName            :

    Line                  : .\InstallMITT.ps1 -Verbose

    PositionMessage       : At line:1 char:1

                           + .\InstallMITT.ps1 -Verbose

                           + ~~~~~~~~~~~~~~~~~~~~~~~~~~

    PSScriptRoot          :

    PSCommandPath         :

    InvocationName        : .\InstallMITT.ps1

    PipelineLength        : 1

    PipelinePosition      : 1

    ExpectingInput        : False

    CommandOrigin         : Runspace

    DisplayScriptPosition :

    How do I get the Line information when the script gets run from a batch file? I'd like to get the arguments in my PS Script so that I can launch the same script again when the script is not run in elevated command prompt.

    Thanks

    Harsha

Page 1 of 1 (5 items)