Matija Sestak
Matija Sestak

Reputation: 423

How to get/add/changed allowed server variables of IIS Web Site with PowerShell?

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?

Upvotes: 4

Views: 2192

Answers (3)

David Peden
David Peden

Reputation: 18414

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

Upvotes: 2

Lia
Lia

Reputation: 523

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>

Upvotes: 2

Matija Sestak
Matija Sestak

Reputation: 423

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.

Upvotes: 1

Related Questions