mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-27 09:08:58 +01:00
Update check-xml-file.ps1
This commit is contained in:
parent
3dbc044948
commit
16526f3a06
@ -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])"
|
||||
|
Loading…
Reference in New Issue
Block a user