param( [Parameter(Mandatory=$true,HelpMessage="Base URL of the site (e.g. https://www.example.com)")] [ValidateNotNullOrEmpty()] [string]$BaseUrl, [Parameter(Mandatory=$true,HelpMessage="Root folder of the site on disk")] [ValidateScript({Test-Path $_ -PathType 'Container'})] [string]$RootPath, [Parameter(HelpMessage="Full path to the sitemap file to create")] [string]$OutputFile = (Join-Path -Path $RootPath -ChildPath "sitemap.xml"), [ValidateSet('always','hourly','daily','weekly','monthly','yearly','never')] [string]$ChangeFreq = 'weekly', [ValidateRange(0,1)] [double]$Priority = 0.5 ) # Normalize inputs $BaseUrl = $BaseUrl.TrimEnd('/') $RootPath = (Resolve-Path -Path $RootPath).ProviderPath $OutputFile = (Resolve-Path -Path (Split-Path -Parent $OutputFile) -ErrorAction SilentlyContinue).ProviderPath + "\" + (Split-Path -Leaf $OutputFile) # Define file extensions to include in the sitemap $includeExt = @('*.html','*.htm','*.php','*.asp','*.aspx','*.jsp') # Gather files $files = Get-ChildItem -Path $RootPath -Recurse -File -Include $includeExt -ErrorAction SilentlyContinue if (-not $files) { Write-Warning "No files matching $($includeExt -join ', ') were found under $RootPath." exit 0 } # Prepare XML writer settings $settings = New-Object System.Xml.XmlWriterSettings $settings.Indent = $true $settings.Encoding = [System.Text.Encoding]::UTF8 # Create the sitemap XML $writer = [System.Xml.XmlWriter]::Create($OutputFile, $settings) try { $writer.WriteStartDocument() $writer.WriteStartElement("urlset") $writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9") foreach ($file in $files) { # Compute relative path and URL $relativePath = $file.FullName.Substring($RootPath.Length).TrimStart('\','/') $url = "$BaseUrl/$($relativePath -replace '\\','/')" $writer.WriteStartElement("url") $writer.WriteElementString("loc", $url) # lastmod in W3C Datetime format (ISO 8601) $lastMod = $file.LastWriteTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ") $writer.WriteElementString("lastmod", $lastMod) $writer.WriteElementString("changefreq", $ChangeFreq) $writer.WriteElementString("priority", $Priority.ToString("0.0", [cultureinfo]::InvariantCulture)) $writer.WriteEndElement() # } $writer.WriteEndElement() # $writer.WriteEndDocument() } finally { $writer.Close() } Write-Host "Sitemap generated at: $OutputFile" -ForegroundColor Green