Update check-xml-file.ps1

This commit is contained in:
Markus Fleschutz 2023-12-06 10:12:33 +01:00
parent 3dbc044948
commit 16526f3a06

View File

@ -1,29 +1,28 @@
<#
.SYNOPSIS
Checks the given XML file for validity
Verifies the given XML file
.DESCRIPTION
This PowerShell script checks the given XML file for validity.
.PARAMETER file
.PARAMETER path
Specifies the path to the XML file to check
.EXAMPLE
PS> ./check-xml-file myfile.xml
XML file is valid
PS> ./check-xml-file.ps1 myfile.xml
Valid XML in 'myfile.xml'
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$file = "")
param([string]$path = "")
try {
if ($file -eq "" ) { $file = read-host "Enter path to XML file" }
if ($path -eq "" ) { $path = Read-Host "Enter path to XML file" }
$XmlFile = Get-Item $file
$script:ErrorCount = 0
$XmlFile = Get-Item $path
# Perform the XSD Validation
$script:ErrorCount = 0
$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
@ -33,11 +32,10 @@ try {
$Reader.Close()
if ($script:ErrorCount -gt 0) {
write-warning "Invalid XML file"
exit 1
throw "Invalid XML in '$path'"
}
"✔️ XML file is valid"
"✔️ Valid XML in '$path'"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"