Script for bulk enabling all URL features

If you're self-hosted, click here or scroll all the way to the bottom.

This script will enable all the URL features for all the websites in your Hudu instance. It requires PowerShell 7 to run, it'll try to run with 7, but if it's not installed it will fail. (it also doesn't work on macOS):

You need:

  • api key

  • username

  • password

This does enable all the features, so if there are ones you don't want to enable, you can just remove them from lines 64-72

note: being that the API does not natively handle websites, it's 'pretending' to be a web browser session, in addition to api key, it requires a username/password combo from a user with 2FA not-enforced.

Copy the script to a blank note document and save as a .ps1.

# Relaunch in PowerShell 7 if not already running in pwsh
if ($PSCommandPath -and $PSVersionTable.PSEdition -eq 'Desktop') {
    $pwsh = Get-Command pwsh -ErrorAction SilentlyContinue
    if ($pwsh) {
        Write-Host "Restarting script in PowerShell 7..."
        & $pwsh.Path $PSCommandPath @args
        exit
    } else {
        Write-Host "pwsh (PowerShell 7) not found in PATH. Please install PowerShell 7 and run this script with 'pwsh'."
        exit 1
    }
}

if (-not (Get-Module -ListAvailable -Name huduapi)) {
    Install-Module huduapi -Force -AllowClobber
}
Import-Module huduapi -ErrorAction Stop
Import-Module huduapi
$apikey=$(Read-host "Enter API Key")
$hudu_url = Read-Host "Enter Hudu URL"
# Normalize URL: force https, remove trailing slash
$hudu_url = $hudu_url -replace '^http://', 'https://' -replace '^(?!https://)', 'https://' -replace '/$', ''
$login_email=$(Read-Host "Enter Email used for login")
$login_password=$(Read-Host "Enter Password used for login")
 
$loginUrl = "$hudu_url/users/sign_in"
 
 
Clear-Host
New-HuduAPIKey "$apikey"
New-HuduBaseUrl "$hudu_url"
write-host "Authenticating..."

$huduSession=$null
try {
$auth_response = Invoke-WebRequest -Uri $loginUrl `
  -Method Post `
  -Body @{ "user[email]" = $login_email; "user[password]" = $login_password } `
  -SessionVariable websession `
  -UseBasicParsing
} catch {
    write-error "Sorry, authentication failed. Try again"
    exit 1
}
$all_websites=$(get-huduwebsites)
$cookie = $websession.Cookies.GetCookies($hudu_url)['_hudu_session']
$authSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$authSession.Cookies.Add((New-Object System.Net.Cookie('_hudu_session', $cookie.Value, "/", (New-Object System.Uri($hudu_url)).Host)))
foreach ($website in $all_websites) {
    try {
        $updateUrl = "$hudu_url$($website.url)"
        $editUrl = "$hudu_url$($website.url)/edit"
        $editPage = Invoke-WebRequest -Uri "$updateUrl/edit" -WebSession $authSession -UseBasicParsing
 
        $csrf = [regex]::Match($editPage.Content, 'name="authenticity_token"\s+value="([^"]+)"').Groups[1].Value
        if (-not $csrf) {
            Write-Warning "Could not extract CSRF token for $($website.name)"
            continue
        }
 
        $body = @{
            authenticity_token = $csrf
            _method = "patch"
            "website[name]" = $website.name
            "website[notes]" = $website.notes
            "website[disable_monitor]" = "0"
            "website[disable_dns]" = "0"
            "website[disable_ssl]" = "0"
            "website[disable_whois]" = "0"
            "website[enable_dmarc_tracking]" = "1"
            "website[enable_dkim_tracking]"  = "1"
            "website[enable_spf_tracking]"   = "1"
            "commit" = "Update"
        }
 
        # Patch request to update website settings
        $response = Invoke-WebRequest -Uri $updateUrl -Method Post -WebSession $authSession -Body $body -UseBasicParsing
        write-host "$($response.statuscode) for $updateurl"
        } catch {
        Write-Warning "Failed to update $($website.name): $_"
    }
}
 # Remove sensitive variables
"apikey","hudu_url","login_email","login_password" | ForEach-Object { Remove-Variable -Name $_ -ErrorAction SilentlyContinue }


Self-hosted

If you're running a self-hosted instance, you can run the following command from the Rails console to perform a mass-update.

sudo docker compose exec app rails c
Website.update_all(
  enable_dmarc_tracking: true,
  enable_dkim_tracking: true,
  enable_spf_tracking: true
)
Bulk_Enable_all_URL_features.ps1
3.28KB
5
1 reply