Added Files
This commit is contained in:
37
Powershell/Scripts/check-xml-file.ps1
Normal file
37
Powershell/Scripts/check-xml-file.ps1
Normal file
@@ -0,0 +1,37 @@
|
||||
<#
|
||||
.SYNTAX check-xml-file [<file>]
|
||||
.DESCRIPTION checks the given XML file for validity
|
||||
.LINK https://github.com/fleschutz/PowerShell
|
||||
.NOTES Author: Markus Fleschutz / License: CC0
|
||||
#>
|
||||
|
||||
param($File = "")
|
||||
|
||||
if ($File -eq "" ) {
|
||||
$File = read-host "Enter path to XML file"
|
||||
}
|
||||
|
||||
try {
|
||||
$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
|
||||
}
|
||||
write-host -foregroundColor green "OK - XML file is valid"
|
||||
exit 0
|
||||
} catch {
|
||||
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||
exit 1
|
||||
}
|
||||
Reference in New Issue
Block a user