简体   繁体   English

如何在PowerShell中运行appCmd以将自定义标头添加到默认网站

[英]How to run appCmd in PowerShell to add custom headers to the Default Web Site

Please help me figure out how to properly escape the arguments so they work when calling appcmd inside of powershell. 请帮我弄清楚如何正确地转义参数,以便在PowerShell中调用appcmd时它们可以工作。

My script looks like this: 我的脚本看起来像这样:

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

The output is this when running inside powershell_ise: 在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).

This works from the command line 这可以从命令行工作

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

Thanks for your help! 谢谢你的帮助!

Try it like this: 试试这样:

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

If you use any variables after the --% the have to be environment variables. 如果在--%之后使用任何变量,则必须是环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM