Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

iis

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?

like image 783
Matija Sestak Avatar asked Nov 16 '25 15:11

Matija Sestak


2 Answers

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>
like image 156
Lia Avatar answered Nov 18 '25 14:11

Lia


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
like image 31
David Peden Avatar answered Nov 18 '25 14:11

David Peden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!