提问人:Brian Lyttle 提问时间:8/23/2008 最后编辑:Paul RoubBrian Lyttle 更新时间:11/30/2016 访问量:78875
如何将多个字符串参数传递给 PowerShell 脚本?
How do I pass multiple string parameters to a PowerShell script?
问:
我正在尝试进行一些字符串连接/格式化,但它将所有参数放入第一个占位符中。
法典
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
输出
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
答:
52赞
Paul Roub
8/23/2008
#1
丢失括号和逗号。
将函数调用为:
$s = CreateAppPoolScript "name" "user" "pass"
给:
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
3赞
Steven Murawski
8/23/2008
#2
保罗说得对。
在 PowerShell 中,函数参数不括在括号中。(方法参数仍然存在。
最初的调用只是将一个大数组传递给函数,而不是将所需的三个单独参数传递给函数。
6赞
Emperor XLII
9/3/2008
#3
顺便说一句,使用 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
"@
}
评论