Improved the scripts

This commit is contained in:
Markus Fleschutz 2021-02-10 20:37:37 +01:00
parent 0f4a7b11a6
commit a2b271c2a9
2 changed files with 21 additions and 22 deletions

View File

@ -2,28 +2,31 @@
<# <#
.SYNTAX ./check-mac-address.ps1 [<MAC>] .SYNTAX ./check-mac-address.ps1 [<MAC>]
.DESCRIPTION checks the given MAC address for validity .DESCRIPTION checks the given MAC address for validity
MAC address like 00:00:00:00:00:00 or 00-00-00-00-00-00 or 000000000000
.LINK https://github.com/fleschutz/PowerShell .LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0 .NOTES Author: Markus Fleschutz / License: CC0
#> #>
# Author: Markus Fleschutz param($MAC = "")
# Source: github.com/fleschutz/PowerShell
# License: CC0
param([string]$MAC) function IsMACAddressValid { param([string]$mac)
# MAC-Address like 00:00:00:00:00:00 or 00-00-00-00-00-00 or 000000000000
$RegEx = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9A-Fa-f]{2}){6}$" $RegEx = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9A-Fa-f]{2}){6}$"
if ($mac -match $RegEx) {
return $true
} else {
return $false
}
}
try { try {
if ($MAC -eq "" ) { if ($MAC -eq "" ) {
$MAC = read-host "Enter MAC address to validate" $MAC = read-host "Enter MAC address to validate"
} }
if ($MAC -match $RegEx) { if (IsMACAddressValid $MAC) {
write-output "MAC address $MAC is valid" write-host -foregroundColor green "OK - MAC address $MAC is valid"
exit 0 exit 0
} else { } else {
write-output "MAC address $MAC is NOT valid" write-warning "Invalid MAC address: $MAC"
exit 1 exit 1
} }
} catch { } catch {

View File

@ -6,16 +6,14 @@
.NOTES Author: Markus Fleschutz / License: CC0 .NOTES Author: Markus Fleschutz / License: CC0
#> #>
param ([String]$XmlFilePath) param($File = "")
try { try {
if ($XmlFilePath -eq "" ) { if ($File -eq "" ) {
$XmlFilePath = read-host "Enter path to XML file" $File = read-host "Enter path to XML file"
} }
# Get the file $XmlFile = Get-Item $File
$XmlFile = Get-Item($XmlFilePath)
# Keep count of how many errors there are in the XML file
$script:ErrorCount = 0 $script:ErrorCount = 0
# Perform the XSD Validation # Perform the XSD Validation
@ -27,14 +25,12 @@ try {
while ($Reader.Read()) { } while ($Reader.Read()) { }
$Reader.Close() $Reader.Close()
# Verify the results of the XSD validation
if ($script:ErrorCount -gt 0) { if ($script:ErrorCount -gt 0) {
write-output "XML file is NOT valid" write-warning "Invalid XML file"
exit 1 exit 1
} else {
write-output "XML file is valid"
exit 0
} }
write-host -foregroundColor green "OK - XML file is valid"
exit 0
} catch { } catch {
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1 exit 1