我正在尝试进行一些字符串连接/格式化,但是它将所有参数放入第一个占位符。
代码
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
答案 0 :(得分:45)
丢失括号和逗号。
将您的功能称为:
$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
答案 1 :(得分:4)
顺便说一下,使用PowerShell here-string可能会让您的函数更容易阅读,因为您不需要将所有"
标记加倍:
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
"@
}
答案 2 :(得分:3)
保罗是对的。
在PowerShell中,函数参数未包含在括号中。 (方法参数仍然是。)
您的初始调用只是将一个大数组传递给函数,而不是您想要的三个独立参数。