如何在PowerShell函数中传递参数?
您可以在PowerShell函数中传递参数,并且要捕获这些参数,需要使用参数。通常,当您在函数外部使用变量时,您实际上不需要传递参数,因为变量本身是Public,可以在函数内部访问。但是在某些情况下,我们需要将参数传递给函数,下面的示例说明了如何编写该函数的代码。
传入函数的单个参数,
function writeName($str){
Write-Output "Hi! there .. $str"
}
$name = Read-Host "Enter Name"
writeName($name)在这里,我们在WriteName函数中传递了$name,而函数中的$str变量捕获了参数,因此,在函数内部,您可以使用$str来检索值。
输出结果
Enter Name: PowerShell Hi! there .. PowerShell
要将多个值传递给函数,不能使用其他编程语言方法将多个值传递给参数。下面的例子是错误的方法,
writeName($name1,$name2)
相反,在PowerShell中,您可以使用下面提到的方法来传递多个值。
示例
writeName $name1 $name2
function writeName($str1,$str2){
Write-Output "Hi! there .. $str1 $str2"
}
$name = Read-Host "Enter Name"
$surname = Read-Host "Enter Name"
writeName $name $surname输出结果
Enter Name: Harry Enter Name: Potter Hi! there .. Harry Potter
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短