开发者

Working with PowerShell functions passing and getting values

A PowerShell script works from the command line.

When attempting to pass the parameter in the script then no values are appearing when the function is called to pass the values the rest of the 开发者_StackOverflowprogram I tried using scope and parameters inside the function.


A sample from your script would highly help in giving you an accurate answer. That said, here is a generic answer that shows a script with functions and without functions in PowerShell.

Function Sum {
    Param ([int]$a,[int]$b)

    return $a+$b
}

So, when you have this in a script, you dot source the script. So, you can call the function directly from cmdline. For example

Sum 4 5 will return 9.

Now, without functions, let us say sum.ps1 has the following code:

Param ([int]$a,[int]$b)
$a+$b

You can call, .\sum.ps1 4 5 at the console or whereever, and it returns 9 again.

It is not necessary to use param() at all. You can use $args array to retrieve the parameters. But, it makes sense to bind parameters to a type and give them a name using Param()


Is it that you want to call a specific function in the script? You can do this by .sourcing and then invoking. Here we call the function sign-script defined in utils.ps1, passing the argument somescript.ps1 to the function.

. .\utils.ps1;sign-script somescript.ps1

Note the extra dot at the start of the line

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜