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