param( [Parameter(Mandatory=$true,HelpMessage="Base URL of the site, e.g., https://example.com")] [string]$BaseUrl, [Parameter(Mandatory=$false,HelpMessage="Root directory to scan (defaults to current location)")] [string]$RootPath = (Get-Location).Path, [Parameter(Mandatory=$false,HelpMessage="File extensions to include (comma‑separated, without the dot)")] [string[]]$IncludeExtensions = @('html','htm','php','aspx','jsp','asp','txt','xml'), [Parameter(Mandatory=$false,HelpMessage="Change frequency for all URLs (default: weekly)")] [ValidateSet('always','hourly','daily','weekly','monthly','yearly','never')] [string]$ChangeFreq = 'weekly', [Parameter(Mandatory=$false,HelpMessage="Default priority for all URLs (0.0‑1.0)")] [ValidateRange(0.0,1.0)] [double]$Priority = 0.5, [Parameter(Mandatory=$false,HelpMessage="Path to output sitemap.xml (defaults to RootPath)")] [string]$OutputPath = "$RootPath\sitemap.xml" ) function Convert-ToUrlPath { param( [string]$Path, [string]$Root ) $relative = Resolve-Path -LiteralPath $Path -Relative -BasePath $Root $urlPath = $relative -replace '\\','/' # URL‑encode reserved characters $urlPath = [System.Web.HttpUtility]::UrlPathEncode($urlPath) return $urlPath } function Get-FileLastModUtc { param([string]$Path) $dt = (Get-Item -LiteralPath $Path).LastWriteTimeUtc return $dt.ToString("yyyy-MM-ddTHH:mm:ss'Z'") } # Ensure BaseUrl ends without trailing slash if ($BaseUrl.EndsWith('/')) { $BaseUrl = $BaseUrl.TrimEnd('/') } # Prepare XmlWriter settings $settings = New-Object System.Xml.XmlWriterSettings $settings.Indent = $true $settings.Encoding = [System.Text.UTF8Encoding]::new($false) # omit BOM $writer = [System.Xml.XmlWriter]::Create($OutputPath, $settings) $writer.WriteStartDocument() $writer.WriteStartElement('urlset') $writer.WriteAttributeString('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9') # Enumerate files $filter = '*.*' $files = Get-ChildItem -Path $RootPath -Recurse -File -Include $IncludeExtensions | Where-Object { $ext = $_.Extension.TrimStart('.').ToLower() $IncludeExtensions -contains $ext } foreach ($file in $files) { $urlPath = Convert-ToUrlPath -Path $file.FullName -Root $RootPath $loc = "$BaseUrl/$urlPath" $writer.WriteStartElement('url') $writer.WriteElementString('loc', $loc) $writer.WriteElementString('lastmod', (Get-FileLastModUtc -Path $file.FullName)) $writer.WriteElementString('changefreq', $ChangeFreq) $writer.WriteElementString('priority', $Priority.ToString('0.0')) $writer.WriteEndElement() # url } $writer.WriteEndElement() # urlset $writer.WriteEndDocument() $writer.Flush() $writer.Close() Write-Host "Sitemap generated at:" $OutputPath