2014-02-05 20 views
4

请帮助我弄清楚如何正确地转义参数,以便在调用PowerShell中的appcmd时工作。如何在PowerShell中运行appCmd以将自定义标题添加到默认网站

我的剧本是这样的:内powershell_ise运行时

$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" 

输出是这样的:

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\"']" 

感谢您的帮助!

回答

5

试试这样说:

$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"'] 

如果使用--%后必须环境变量的变量。

+0

感谢您的建议。我收到关于缺少密切报价的错误。 字符串起始: 在行:4 char:180 +&$ appCmd - %set config“%defaultWebSite%”-section:system.webServer/httpProtocol /+""customHeaders.[name='P3P', value ='policyRef = \“/ w3c/p3p.xml \”,CP = \“DSP COR NID OUR COM PRE \''] <<<<” 缺少终结符:“。 在线:4 char:181 –

+0

我从上面列出的工作范例中复制了从'-section'到结尾的所有内容。不过,您可能已经通过PowerShell进行了更改。在您指定的位置 - %PowerShell解析更多像cmd.exe。让我试试摆脱PowerShell解决方案。 –

相关问题