4

I'm trying to make a PS script that creates an IIS web page, configures a reverse proxy via URL Rewrite and I'm stuck on how to add allowed server variables via PowerShell.

enter image description here

Does anyone know how to add these variables via powershell for the IIS website?

1
  • 1
    Could you pls post your solution as the answer to end this case so that it can easy to be found and help others. Thanks Commented Dec 2, 2021 at 1:30

3 Answers 3

2

FYI adding server variable on server level by powershell:

Add-WebConfiguration /system.webServer/rewrite/allowedServerVariables -atIndex 0 -value @{name="RESPONSE_SERVER"}

this will add the following configuration in applicationhost.config

<system.webServer>
...
    <rewrite>
    ...
        <allowedServerVariables>
            <add name="RESPONSE_SERVER" />
        </allowedServerVariables>
    ...
    </rewrite>
</system.webServer>
Sign up to request clarification or add additional context in comments.

Comments

2

Updated 2024/09/24

this answer works with PowerShell 7+

  1. import the iis module
Import-Module IISAdministration
  1. create the following two functions:
function Enable-IisServerVariable($variable) {
    $section = Get-IISConfigSection -SectionPath "system.webServer/rewrite/allowedServerVariables"
    $collection = Get-IISConfigCollection -ConfigElement $section
    $existingVariables = $collection | ForEach-Object { $_.Attributes["name"].Value }

    if (-not ($existingVariables -contains $variable)) {
        New-IISConfigCollectionElement -ConfigCollection $collection -ConfigAttribute @{"Name" = $variable}
        Write-Host "Server variable '$variable' has been added."
    } else {
        Write-Host "Server variable '$variable' is already enabled."
    }
}

function Enable-ReverseProxyHeaderForwarding() {
    foreach ($variable in @("HTTP_X_FORWARDED_PROTO", "HTTP_X_FORWARDED_HOST", "HTTP_X_FORWARDED_FOR")) {
        Enable-IisServerVariable $variable
    }
}
  1. execute the function:
Enable-ReverseProxyHeaderForwarding

Original Answer

as noted in @matija's answer, you will need to ensure you have the web admin module loaded:

Import-Module WebAdministration

then, building on @lia's answer, you can define a function to add multiple variables in an idempotent fashion like this:

function Enable-ReverseProxyHeaderForwarding() {
    $requiredVariables = @("HTTP_X_FORWARDED_PROTO", "HTTP_X_FORWARDED_HOST", "HTTP_X_FORWARDED_FOR")
    $existingVariables = Get-WebConfiguration -Filter "/system.webServer/rewrite/allowedServerVariables/add" -PSPath "IIS:\Sites\" | ForEach-Object { $_.name }

    foreach ($variable in $requiredVariables) {
        if (-not ($existingVariables -contains $variable)) {
            Add-WebConfiguration "/system.webServer/rewrite/allowedServerVariables" -value @{name = $variable }
        }
    }
}

and then execute the function:

Enable-ReverseProxyHeaderForwarding

Comments

1

UPDATE: I have solved this problem

Step 1. Unlock server configuration section

%windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/rewrite/allowedServerVariables

Now it's posible to get allowed server variables with

import-module WebAdministration

$webSiteName = "your-site-name"
$allowedServerVariables = Get-WebConfigurationProperty -PSPath "MACHINE/WEBROOT/APPHOST/${webSiteName}" -filter "system.webServer/rewrite/allowedServerVariables/add" -Name name

Write-Host $allowedServerVariables.Value

Example for adding server variables

import-module WebAdministration

$webSiteName = "your-web-site-name"
add-webconfigurationproperty -pspath "iis:\sites\${webSiteName}"  -filter "system.webserver/rewrite/allowedservervariables" -name "." -value @{name='HTTP_X_ORIGINAL_ACCEPT_ENCODING'}

UPDATE 2 Better solution without PowerShell is to add allowed server variables globally on IIS web server. So each variable will be automatically inherited from new web site.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.