如何在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 50多岁生日简短祝福语
16 横幅家庭生日祝福语简短
17 朋友入伙搬家祝福语简短
18 生日祝福语简短霸屏