6

请帮助我弄清楚如何正确转义参数,以便它们在 powershell 内调用 appcmd 时起作用。

我的脚本如下所示:

$defaultWebSite = "Default Web Site"
$appCmd = "C:\windows\system32\inetsrv\appcmd.exe"
$addHeaderP3P = "set config ""$defaultWebSite"" -section:system.webServer/httpProtocol /+""customHeaders.[name='P3P',value='policyRef=`\`"/w3c/p3p.xml`\`",CP=`\`"DSP COR NID OUR COM PRE`\`"']`""

Write-Output "Here's the argument string: " $addHeaderP3P



Write-Output "`nInvoke Result:"
Invoke-Expression "$appCmd $addHeaderP3P"


Write-Output "`n& Result:"
& $appCmd --%"$addHeaderP3P"

在 powershell_ise 中运行时的输出是这样的:

PS C:\Users\robert.bratton> D:\Junk\p3pheader.ps1
Here's the argument string: 
set config "Default Web Site" -section:system.webServer/httpProtocol /+"customHeaders.[name='P3P',value='policyRef=\"/w3c/p3p.xml\",CP=\"DSP COR NID OUR COM PRE\"']"

Invoke Result:
Failed to process input: The parameter 'COR' must begin with a / or - (HRESULT=80070057).


& Result:
Failed to process input: The parameter 'NID' must begin with a / or - (HRESULT=80070057).

这适用于命令行

"C:\windows\system32\inetsrv\appcmd.exe" set config "Default Web Site" -section:system.webServer/httpProtocol /+"customHeaders.[name='P3P',value='policyRef=\"/w3c/p3p.xml\",CP=\"DSP COR NID OUR COM PRE\"']"

谢谢你的帮助!

4

1 回答 1

9

试试这样:

$env:defaultWebSite = "Default Web Site"
$appCmd = "C:\windows\system32\inetsrv\appcmd.exe"

& $appCmd --% set config "%defaultWebSite%" -section:system.webServer/httpProtocol /+customHeaders.[name='P3P',value='policyRef="/w3c/p3p.xml",CP="DSP COR NID OUR COM PRE"']

如果您在 之后使用任何变量,--%则必须是环境变量。

于 2014-02-05T19:00:11.127 回答