mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-12 19:14:33 +01:00
43 lines
1.1 KiB
PowerShell
Executable File
43 lines
1.1 KiB
PowerShell
Executable File
<#
|
|
.SYNOPSIS
|
|
check-xml-file [<file>]
|
|
.DESCRIPTION
|
|
Checks the given XML file for validity.
|
|
.EXAMPLE
|
|
PS> .\check-xml-file.ps1 myfile.xml
|
|
.NOTES
|
|
Author: Markus Fleschutz · License: CC0
|
|
.LINK
|
|
https://github.com/fleschutz/PowerShell
|
|
#>
|
|
|
|
param([string]$File = "")
|
|
|
|
try {
|
|
if ($File -eq "" ) { $File = read-host "Enter path to XML file" }
|
|
|
|
$XmlFile = Get-Item $File
|
|
|
|
$script:ErrorCount = 0
|
|
|
|
# Perform the XSD Validation
|
|
$ReaderSettings = New-Object -TypeName System.Xml.XmlReaderSettings
|
|
$ReaderSettings.ValidationType = [System.Xml.ValidationType]::Schema
|
|
$ReaderSettings.ValidationFlags = [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessInlineSchema -bor [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessSchemaLocation
|
|
$ReaderSettings.add_ValidationEventHandler({ $script:ErrorCount++ })
|
|
$Reader = [System.Xml.XmlReader]::Create($XmlFile.FullName, $ReaderSettings)
|
|
while ($Reader.Read()) { }
|
|
$Reader.Close()
|
|
|
|
if ($script:ErrorCount -gt 0) {
|
|
write-warning "Invalid XML file"
|
|
exit 1
|
|
}
|
|
|
|
"✔️ XML file is valid"
|
|
exit 0
|
|
} catch {
|
|
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
exit 1
|
|
}
|