Invoking Function in PowerShell via String

前端 未结 2 1967
一个人的身影
一个人的身影 2020-12-15 06:21

I\'m trying to Invoke a Powershell function \"dynamically\" using a string. Example

$functionToInvoke = \"MyFunctionName\";

Invoke-Function $functionToInvok         


        
相关标签:
2条回答
  • 2020-12-15 06:26

    If you're wanting to variableize the whlole thing:

    function myfunctionname {write-host "$($args[0]) $($args[1])"}
    $arg1 = "scripts"
    $arg2 = "test"
    
    $functionToInvoke = "MyFunctionName";
    
    
    invoke-expression  "$functionToInvoke $arg1 $arg2"
    
    scripts test
    
    0 讨论(0)
  • 2020-12-15 06:31

    You can do this:

     &"MyFunctionName" $arg1 $arg2
    
    0 讨论(0)
提交回复
热议问题