ps/Modules/Alkami.PowerShell.IIS/Public/Set-ServerResponseHeaders.ps1

52 lines
1.7 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Set-ServerResponseHeaders {
<#
.SYNOPSIS
This function sets the server's reponse headers correctly
#>
[CmdletBinding()]
Param()
$logLead = (Get-LogLeadName);
# Set Server Response Headers
$mgr = Get-IISServerManager
$config = $mgr.GetWebConfiguration("/")
$httpProtocolSection = $config.GetSection("system.webServer/httpProtocol")
$customHeadersCollection = $httpProtocolSection.GetCollection("customHeaders")
$serverReponseHeader = @()
foreach($customHeader in $customHeadersCollection) {
if ($customHeader.RawAttributes.value -match "ASP.NET") {
Write-Output ("$logLead : Detected X-Powered-By response header. Removing it.")
$customHeader.Delete()
}
if ($customHeader.RawAttributes.name -match "X-SVR") {
$serverReponseHeader += $customHeader
}
}
## If no X-SVR element found, go ahead and create one with the local machine name
if (Test-IsCollectionNullOrEmpty $serverReponseHeader) {
Write-Output ("$logLead : X-SVR response header not found. Creating it.")
if ($env:COMPUTERNAME -like 'alka*') {
# Legacy naming convention
[Regex]$regex = "(\w{1}\d+$)"
$serverResponseValue = ($regex.Matches($env:COMPUTERNAME).Groups | Select-Object -First 1).Value
}
else {
# Use entire machine name
$serverResponseValue = $env:COMPUTERNAME
}
$addElement = $customHeadersCollection.CreateElement("add")
$addElement["name"] = "X-SVR"
$addElement["value"] = $serverResponseValue
$customHeadersCollection.Add($addElement) | Out-Null
}
Save-IISServerManagerChanges $mgr
}