2016-11-23 76 views
0

換句話說,我該如何獲得腳本本身的命令行?我知道$PSBoundParameters,但它不一樣。我只想獲取包含傳入參數的字符串。

我該怎麼做?

+0

「我只想得到包含傳入參數是字符串。」 - '$ *'不這樣做。 – user2357112

+0

當您可以利用PowerShell的內置參數解析功能時,您爲什麼要這麼做? –

+0

@Bill_Stewart - 我需要用相同的命令行參數和其他幾個參數調用powershell.exe。 – mark

回答

2
$MyInvocation.Line 

閱讀about_Automatic_Variables

$MyInvocation 
    Contains an information about the current command, such as the name, 
    parameters, parameter values, and information about how the command was 
    started, called, or "invoked," such as the name of the script that called 
    the current command. 

    $MyInvocation is populated only for scripts, function, and script blocks. 
+0

這很好,謝謝。不知道。 – mark

1

get-help about_Automatic_Variables

$Args 
    Contains an array of the undeclared parameters and/or parameter 
    values that are passed to a function, script, or script block. 
    When you create a function, you can declare the parameters by using the 
    param keyword or by adding a comma-separated list of parameters in 
    parentheses after the function name. 

    In an event action, the $Args variable contains objects that represent 
    the event arguments of the event that is being processed. This variable 
    is populated only within the Action block of an event registration 
    command. The value of this variable can also be found in the SourceArgs 
    property of the PSEventArgs object (System.Management.Automation.PSEventArgs) 
    that Get-Event returns. 

例子:

test.ps1

param (
) 

Write-Output "Args:" 
$args 

輸出:

PS L:\test> .\test.ps1 foo bar, 'this is extra' 
Args: 
foo 
bar 
this is extra 
+0

我的腳本有很多聲明的參數。 – mark

相關問題