string 如何将多个字符串参数传递给 PowerShell 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass multiple string parameters to a PowerShell script?
提问by Brian Lyttle
I am trying to do some string concatenation/formatting, but it's putting all the parameters into the first placeholder.
我正在尝试进行一些字符串连接/格式化,但它将所有参数放入第一个占位符中。
Code
代码
function CreateAppPoolScript([string]$AppPoolName, [string]$AppPoolUser, [string]$AppPoolPass)
{
# Command to create an IIS application pool
$AppPoolScript = "cscript adsutil.vbs CREATE ""w3svc/AppPools/$AppPoolName"" IIsApplicationPool`n"
$AppPoolScript += "cscript adsutil.vbs SET ""w3svc/AppPools/$AppPoolName/WamUserName"" ""$AppPoolUser""`n"
$AppPoolScript += "cscript adsutil.vbs SET ""w3svc/AppPools/$AppPoolName/WamUserPass"" ""$AppPoolPass""`n"
$AppPoolScript += "cscript adsutil.vbs SET ""w3svc/AppPools/$AppPoolName/AppPoolIdentityType"" 3"
return $AppPoolScript
}
$s = CreateAppPoolScript("name", "user", "pass")
write-host $s
Output
输出
cscript adsutil.vbs CREATE "w3svc/AppPools/name user pass" IIsApplicationPool
cscript adsutil.vbs SET "w3svc/AppPools/name user pass/WamUserName" ""
cscript adsutil.vbs SET "w3svc/AppPools/name user pass/WamUserPass" ""
cscript adsutil.vbs SET "w3svc/AppPools/name user pass/AppPoolIdentityType" 3
回答by Paul Roub
Lose the parentheses and commas.
去掉括号和逗号。
Calling your function as:
将您的函数调用为:
$s = CreateAppPoolScript "name" "user" "pass"
gives:
给出:
cscript adsutil.vbs CREATE "w3svc/AppPools/name" IIsApplicationPool
cscript adsutil.vbs SET "w3svc/AppPools/name/WamUserName" "user"
cscript adsutil.vbs SET "w3svc/AppPools/name/WamUserPass" "pass"
cscript adsutil.vbs SET "w3svc/AppPools/name/AppPoolIdentityType" 3
回答by Emperor XLII
By the way, using a PowerShell here-stringmight make your function a little easier to read as well, since you won't need to double up all the "
-marks:
顺便说一句,使用 PowerShell here-string也可能使您的函数更易于阅读,因为您不需要将所有"
-marks 加倍:
function CreateAppPoolScript([string]$AppPoolName, [string]$AppPoolUser, [string]$AppPoolPass)
{
# Command to create an IIS application pool
return @"
cscript adsutil.vbs CREATE "w3svc/AppPools/$AppPoolName" IIsApplicationPool
cscript adsutil.vbs SET "w3svc/AppPools/$AppPoolName/WamUserName" "$AppPoolUser"
cscript adsutil.vbs SET "w3svc/AppPools/$AppPoolName/WamUserPass" "$AppPoolPass"
cscript adsutil.vbs SET "w3svc/AppPools/$AppPoolName/AppPoolIdentityType" 3
"@
}
回答by Steven Murawski
Paul's right.
In PowerShell, function parameters are not enclosed in parenthesis. (Method parameters still are.)
Your initial call was just passing one big array to the function, rather than the three separate parameters you wanted.
保罗说得对。
在 PowerShell 中,函数参数不包含在括号中。(方法参数仍然是。)
您最初的调用只是将一个大数组传递给函数,而不是您想要的三个单独的参数。