param( [Parameter(Mandatory=$true, Position=0)] [string]$SiteRoot, # Base URL, e.g. https://www.example.com [Parameter(Mandatory=$true, Position=1)] [ValidateScript({Test-Path $_ -PathType Container})] [string]$Path, # Physical path to the web root [ValidateSet('always','hourly','daily','weekly','monthly','yearly','never')] [string]$ChangeFreq = 'weekly', [ValidateRange(0.0,1.0)] [double]$Priority = 0.5, [string[]]$IncludeExtensions = @('.html','.htm','.php','.aspx','.asp','.jsp'), [switch]$Recursive ) # Normalize inputs $SiteRoot = $SiteRoot.TrimEnd('/') $Path = (Resolve-Path -Path $Path).Path # Gather files $searchOption = if ($Recursive) { [System.IO.SearchOption]::AllDirectories } else { [System.IO.SearchOption]::TopDirectoryOnly } $files = Get-ChildItem -Path $Path -File -Include $IncludeExtensions -Recurse:$Recursive if (-not $files) { Write-Warning "No files matching extensions $($IncludeExtensions -join ', ') were found in $Path." exit 0 } # Create XML writer settings $xmlSettings = New-Object System.Xml.XmlWriterSettings $xmlSettings.Indent = $true $xmlSettings.Encoding = [System.Text.UTF8Encoding]::new($false) $sitemapPath = Join-Path -Path $Path -ChildPath 'sitemap.xml' $writer = [System.Xml.XmlWriter]::Create($sitemapPath, $xmlSettings) try { $writer.WriteStartDocument() $writer.WriteStartElement('urlset') $writer.WriteAttributeString('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9') foreach ($file in $files) { $relativePath = $file.FullName.Substring($Path.Length).TrimStart('\','/') $urlPath = $relativePath -replace '\\','/' $loc = "$SiteRoot/$urlPath" $writer.WriteStartElement('url') $writer.WriteElementString('loc', $loc) $lastMod = $file.LastWriteTimeUtc.ToString('yyyy-MM-dd') $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() $writer.Dispose() } Write-Output "Sitemap generated at: $sitemapPath"