2016-11-27 6 views
2

parametersfunctionに送信し、そのうち1つをarrayに送信したいとします。関数内デバッガ経由で見たときにfunction配列は、いくつかの項目であるコール前 はarrayempty\nullです:機能に送信された後に配列が空になった理由

$arr = New-Object System.Collections.ArrayList 
$arr.Add("test1") 
GetProcessOutput -exeFile "c:\file.exe" -args $arr 

function GetProcessOutput($exeFile, $args) 
{ 
    # here my $args is empty -> children could not be evaluated 
} 
+2

'$ args' - >' $ { $ args自動変数と衝突しない他のもの} ' – PetSerAl

+0

また、スクリプトの可読性を向上させるために、関数呼び出しの上に関数定義を移動する必要があります。 – user4317867

答えて

4

$argsは、ユーザーのためにargs名を使用して防止されることを意味し、automatic variableです定義された変数。

他の名前を使用し、それがうまくいく:

function GetProcessOutput([string]$exeFile,[array]$arguments) 
{ 
    # $arguments will work just fine 
} 

about_Variables help fileから:

 There are several different types of variables in Windows 
PowerShell. 

-- User-created variables: User-created variables are created and 
    maintained by the user. By default, the variables that you create at 
    the Windows PowerShell command line exist only while the Windows 
    PowerShell window is open, and they are lost when you close the window. 
    To save a variable, add it to your Windows PowerShell profile. You can 
    also create variables in scripts with global, script, or local scope.  

-- Automatic variables: Automatic variables store the state of 
    Windows PowerShell. These variables are created by Windows PowerShell, 
    and Windows PowerShell changes their values as required to maintain 
    their accuracy. Users cannot change the value of these variables. 
    For example, the $PSHome variable stores the path to the Windows 
    PowerShell installation directory. 
+1

うまくやった。残念なことに、 '$ args'に関して「使用を妨げられた」とは、「あなたはこの名前でパラメータを宣言することは自由ですが、無視されます」、おそらくバグです。 これに対して、 '$ PSHOME'というパラメータの名前をつけようとすると、' PSHOME変数が読み込み専用であるか定数であるため、上書きできません。 – mklement0

関連する問題