-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathConfirm-Schema.ps1
More file actions
65 lines (57 loc) · 1.49 KB
/
Confirm-Schema.ps1
File metadata and controls
65 lines (57 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<#
.SYNOPSIS
Validates an XML file against a given schema definition file.
.PARAMETER XmlPath
Path of the XML file to validate.
.PARAMETER XsdPath
Path of the XSD schema definnition file used to validate the XML file.
.PARAMETER Namespace
The namespace to add to the validation.
#>
# CmdletBinding adds -Verbose functionality, SupportsShouldProcess adds -WhatIf
[CmdletBinding(SupportsShouldProcess = $true)]
[OutputType([bool])]
param
(
[Parameter(Mandatory)]
[ValidateScript({ Test-Path -Path $_ })]
[ValidatePattern('\.xml')]
[string] $XmlPath,
[Parameter(Mandatory)]
[ValidateScript({ Test-Path -Path $_ })]
[ValidatePattern('\.xsd')]
[string] $XsdPath,
[string] $Namespace = 'http://schemas.microsoft.com/office/onenote/2013/onenote'
)
Begin
{
<#
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.microsoft.com/office/onenote/2013/onenote"
xmlns="http://schemas.microsoft.com/office/onenote/2013/onenote"
elementFormDefault="qualified">
#>
}
Process
{
try
{
[xml]$xml = Get-Content $XmlPath
$xml.Schemas.Add($Namespace, $XsdPath) | Out-Null
$xml.Validate($null)
Write-Verbose "Successfully validated $XmlPath against schema ($XsdPath)"
$result = $true
}
catch
{
$err = $_.Exception.Message
Write-Verbose "Failed to validate $XmlPath against schema ($XsdPath)`nDetails: $err"
$_
$result = $false
}
finally
{
$result
}
}