簡體   English   中英

通過String在PowerShell中調用函數

[英]Invoking Function in PowerShell via String

我正在嘗試使用字符串“動態”調用Powershell函數。

$functionToInvoke = "MyFunctionName";

Invoke-Function $functionToInvoke $arg1 $arg2  #  <- what I would like to do

有沒有辦法用PowerShell 2.0實現這一目標?

你可以這樣做:

 &"MyFunctionName" $arg1 $arg2

如果你想改變這個白痴的事情:

function myfunctionname {write-host "$($args[0]) $($args[1])"}
$arg1 = "scripts"
$arg2 = "test"

$functionToInvoke = "MyFunctionName";


invoke-expression  "$functionToInvoke $arg1 $arg2"

scripts test

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM