param( [Parameter(Mandatory=$true,HelpMessage="Root folder of your website files.")] [ValidateScript({Test-Path $_ -PathType Container})] [string]$Path, [Parameter(Mandatory=$true,HelpMessage="Base URL of your site, e.g., https://example.com")] [ValidatePattern('^https?://')] [string]$BaseUrl, [Parameter(HelpMessage="Full path for the sitemap output file. Defaults to sitemap.xml in the root folder.")] [string]$OutputFile, [Parameter(HelpMessage="File extensions to include (case‑insensitive).")] [string[]]$IncludeExtensions = @('.html', '.htm', '.php', '.aspx'), [Parameter(HelpMessage="File extensions to exclude (case‑insensitive).")] [string[]]$ExcludeExtensions = @('.css', '.js', '.json', '.xml', '.txt') ) # Resolve and normalize paths $rootPath = (Resolve-Path -Path $Path).ProviderPath if (-not $OutputFile) { $OutputFile = Join-Path -Path $rootPath -ChildPath 'sitemap.xml' } $OutputFile = (Resolve-Path -Path $OutputFile -ErrorAction SilentlyContinue).ProviderPath ` ? (Resolve-Path -Path $OutputFile).ProviderPath ` : $OutputFile # Prepare extension hashsets for fast lookup $includeSet = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::InvariantCultureIgnoreCase) $IncludeExtensions | ForEach-Object { $includeSet.Add($_) | Out-Null } $excludeSet = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::InvariantCultureIgnoreCase) $ExcludeExtensions | ForEach-Object { $excludeSet.Add($_) | Out-Null } # Gather files $files = Get-ChildItem -Path $rootPath -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $ext = $_.Extension $includeSet.Contains($ext) -and -not $excludeSet.Contains($ext) } if (-not $files) { Write-Warning "No files matching the criteria were found under '$rootPath'." exit 0 } # XML writer settings $xmlSettings = New-Object System.Xml.XmlWriterSettings $xmlSettings.Indent = $true $xmlSettings.Encoding = [System.Text.Encoding]::UTF8 try { $writer = [System.Xml.XmlWriter]::Create($OutputFile, $xmlSettings) $writer.WriteStartDocument() $writer.WriteStartElement('urlset', 'http://www.sitemaps.org/schemas/sitemap/0.9') foreach ($file in $files) { # Compute relative path and URL $relativePath = $file.FullName.Substring($rootPath.Length).TrimStart('\','/') $urlPath = $relativePath -replace '\\','/' # Ensure URL is properly escaped (encode spaces and other unsafe chars) $encodedPath = [System.Web.HttpUtility]::UrlPathEncode($urlPath) $loc = ($BaseUrl.TrimEnd('/') + '/' + $encodedPath).TrimEnd('/') # Format last modification date in UTC ISO 8601 (e.g., 2024-03-15T12:34:56Z) $lastMod = $file.LastWriteTimeUtc.ToString('yyyy-MM-ddTHH:mm:ssZ') $writer.WriteStartElement('url') $writer.WriteElementString('loc', $loc) $writer.WriteElementString('lastmod', $lastMod) $writer.WriteEndElement() # } $writer.WriteEndElement() # $writer.WriteEndDocument() } finally { if ($writer) { $writer.Close() } } Write-Host "Sitemap generated successfully at: $OutputFile"