param( [Parameter(Mandatory = $true)] [string]$RootPath, [Parameter(Mandatory = $true)] [string]$BaseUrl, [string]$OutputFile = "sitemap.xml", [ValidateSet('always','hourly','daily','weekly','monthly','yearly','never')] [string]$ChangeFreq = 'weekly', [ValidateRange(0.0,1.0)] [float]$Priority = 0.5 ) # Ensure root path exists if (-not (Test-Path -LiteralPath $RootPath -PathType Container)) { Write-Error "RootPath '$RootPath' does not exist or is not a directory." exit 1 } # Normalize BaseUrl if ($BaseUrl -notmatch '^https?://') { Write-Error "BaseUrl must start with http:// or https://" exit 1 } if ($BaseUrl[-1] -ne '/') { $BaseUrl += '/' } # Prepare file list $extensions = '*.html','*.htm','*.php','*.aspx','*.asp','*.jsp' $files = Get-ChildItem -Path $RootPath -Recurse -File -Include $extensions # Create XML writer settings $settings = New-Object System.Xml.XmlWriterSettings $settings.Indent = $true $settings.Encoding = [System.Text.Encoding]::UTF8 $writer = [System.Xml.XmlWriter]::Create($OutputFile, $settings) $writer.WriteStartDocument() $writer.WriteStartElement('urlset') $writer.WriteAttributeString('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9') foreach ($file in $files) { $relativePath = Resolve-Path -LiteralPath $file.FullName -Relative -RelativeBase $RootPath # Replace backslashes with forward slashes and URL-encode spaces $urlPath = ($relativePath -replace '\\','/').TrimStart('/') $escapedPath = [System.Web.HttpUtility]::UrlPathEncode($urlPath).Replace('+','%20') $fullUrl = $BaseUrl + $escapedPath $lastMod = $file.LastWriteTimeUtc.ToString('yyyy-MM-ddTHH:mm:ssK') $writer.WriteStartElement('url') $writer.WriteElementString('loc', $fullUrl) $writer.WriteElementString('lastmod', $lastMod) $writer.WriteElementString('changefreq', $ChangeFreq) $writer.WriteElementString('priority', $Priority.ToString('0.0', [System.Globalization.CultureInfo]::InvariantCulture)) $writer.WriteEndElement() # url } $writer.WriteEndElement() # urlset $writer.WriteEndDocument() $writer.Flush() $writer.Close() Write-Host "Sitemap generated at: $(Resolve-Path -LiteralPath $OutputFile)"