param ( [Parameter(Mandatory = $true)] [string]$xmlFolderPath ) $xmlFiles = Get-ChildItem -Path $xmlFolderPath -Force | Where-Object -FilterScript { $_.Extension -eq ".xml" } if ($xmlFiles.Count -eq 0) { Write-Warning "No XML files found in the specified path" exit } $handler = [System.Xml.Schema.ValidationEventHandler] { $copy = $_ switch ($args.Severity) { Error { Write-Error "ERROR: line $($copy.Exception.LineNumber)" Write-Error "position $($copy.Exception.LinePosition)" Write-Error $copy.Message throw } Warning { Write-Warning "Warning:: " + $copy.Message break } } } try { Push-Location (Split-Path $xmlFolderPath) $isValid = $true $failedFiles = @() $xsd = "https://raw.githubusercontent.com/Azure-Samples/active-directory-b2c-custom-policy-starterpack/master/TrustFrameworkPolicy_0.3.0.0.xsd" Invoke-WebRequest -Uri $xsd -OutFile "TrustFrameworkPolicy_0.3.0.0.xsd" foreach ($xmlfile in $xmlFiles) { Write-Host "Validating $($xmlFolderPath + $xmlfile.Name)" -ForegroundColor Cyan $settings = new-object System.Xml.XmlReaderSettings $null = $settings.Schemas.Add("http://schemas.microsoft.com/online/cpim/schemas/2013/06", "$(Get-Location)/TrustFrameworkPolicy_0.3.0.0.xsd") $null = $settings.ValidationType = [System.Xml.ValidationType]::Schema $settings.add_ValidationEventHandler($handler) $reader = [System.Xml.XmlReader]::Create($xmlfile, $settings) $document = new-object System.Xml.XmlDocument try { $null = $document.Load($reader) } catch { $isValid = $false $failedFiles += $xmlfile.Name } $null = $reader.Close() } if ($isValid -eq $false) { Write-Error "Validation failed for $($failedFiles)" exit 1 } } catch { throw } finally { Remove-Item -Path "TrustFrameworkPolicy_0.3.0.0.xsd" Pop-Location }