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

12,320

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.

Share:
12,320
Robert Bratton
Author by

Robert Bratton

I'm an experienced Information Technology professional with a background in software engineering, database design, network engineering and system administration.

Updated on June 20, 2022

Comments

  • Robert Bratton
    Robert Bratton almost 2 years

    Please help me figure out how to properly escape the arguments so they work when calling appcmd inside of powershell.

    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:

    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!

  • Robert Bratton
    Robert Bratton about 10 years
    Thanks for the suggestion. I get an error about a missing close quote. The string starting: At line:4 char:180 + & $appCmd --% set config "%defaultWebSite%" -section:system.webServer/httpProtocol /+""customHeaders.[name='P3P',value='policyRef=\"/w3c/p3p.xm‌​l\",CP=\"DSP COR NID OUR COM PRE\"'] <<<< " is missing the terminator: ". At line:4 char:181
  • Keith Hill
    Keith Hill about 10 years
    I copied everything from -section to the end from the example you listed above as working. However you may have made changes to make that work from PowerShell. At the point you specify --% PowerShell parses more like cmd.exe. Let me take a crack to getting rid of the PowerShell workarounds.