Added check-ipv4-address.ps1 and check-mac-address.ps1

This commit is contained in:
Markus Fleschutz 2020-12-22 10:10:16 +00:00
parent d8b9432c44
commit 5404722dc8
5 changed files with 100 additions and 33 deletions

View File

@ -7,6 +7,9 @@ List of Scripts
---------------
The following PowerShell scripts can be found in the [Scripts/](Scripts/) subfolder:
* [check-ipv4-address.ps1](Scripts/check-ipv4-address.ps1) - checks the given IPv4 address for validity
* [check-mac-address.ps1](Scripts/check-mac-address.ps1) - checks the given MAC address for validity
* [check-xml-file.ps1](Scripts/check-xml-file.ps1) - checks the given XML file for validity
* [clone-repos.ps1](Scripts/clone-repos.ps1) - clones well-known Git repositories
* [configure-git.ps1](Scripts/configure-git.ps1) - sets up the Git configuration
* [download.ps1](Scripts/download.ps1) - downloads the file/directory from the given URL
@ -54,7 +57,6 @@ The following PowerShell scripts can be found in the [Scripts/](Scripts/) subfol
* [translate-file.ps1](Scripts/translate-file.ps1) - translates the given file from source to target language
* [translate-text.ps1](Scripts/translate-text.ps1) - translates the given text into other languages
* [txt2wav.ps1](Scripts/txt2wav.ps1) - converts text into a audio .WAV file
* [validate-xml.ps1](Scripts/validate-xml.ps1) - validates the given XML file
* [weather.ps1](Scripts/weather.ps1) - prints the current weather forecast
* [weather-alert.ps1](Scripts/weather-alert.ps1) - checks the current weather for critical values
* [weather-report.ps1](Scripts/weather-report.ps1) - prints the local weather report

28
Scripts/check-ipv4-address.ps1 Executable file
View File

@ -0,0 +1,28 @@
#!/snap/bin/powershell
# Syntax: ./check-ipv4-address.ps1 [<address>]
# Description: checks the given IPv4 address for validity
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0
param([string]$Addr)
# IPv4-Address like 192.168.178.1
$RegEx = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
try {
if ($Addr -eq "" ) {
$Addr = read-host "Enter IPv4 address to validate"
}
if ($Addr -match $RegEx) {
write-output "IPv4 address $Addr is valid"
exit 0
} else {
write-output "IPv4 address $Addr is NOT valid"
exit 1
}
} catch {
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}

28
Scripts/check-mac-address.ps1 Executable file
View File

@ -0,0 +1,28 @@
#!/snap/bin/powershell
# Syntax: ./check-mac-address.ps1 [<MAC>]
# Description: checks the given MAC address for validity
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0
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}$"
try {
if ($MAC -eq "" ) {
$MAC = read-host "Enter MAC address to validate"
}
if ($MAC -match $RegEx) {
write-output "MAC address $MAC is valid"
exit 0
} else {
write-output "MAC address $MAC is NOT valid"
exit 1
}
} catch {
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}

41
Scripts/check-xml-file.ps1 Executable file
View File

@ -0,0 +1,41 @@
#!/snap/bin/powershell
# Syntax: ./check-xml-file [<file>]
# Description: checks the given XML file for validity
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0
param ([String]$XmlFilePath)
try {
if ($XmlFilePath -eq "" ) {
$XmlFilePath = read-host "Enter path to XML file"
}
# Get the file
$XmlFile = Get-Item($XmlFilePath)
# Keep count of how many errors there are in the XML file
$script:ErrorCount = 0
# Perform the XSD Validation
$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
$ReaderSettings.add_ValidationEventHandler({ $script:ErrorCount++ })
$Reader = [System.Xml.XmlReader]::Create($XmlFile.FullName, $ReaderSettings)
while ($Reader.Read()) { }
$Reader.Close()
# Verify the results of the XSD validation
if ($script:ErrorCount -gt 0) {
write-output "XML file is NOT valid"
exit 1
} else {
write-output "XML file is valid"
exit 0
}
} catch {
write-warning "$($MyInvocation.MyCommand.Name) - Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
exit 1
}

View File

@ -1,32 +0,0 @@
#!/snap/bin/powershell
function Validate-Xml {
param ([string]$XmlFilePath)
try {
# Get the file
$XmlFile = Get-Item($XmlFilePath)
# Keep count of how many errors there are in the XML file
$script:ErrorCount = 0
# Perform the XSD Validation
$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
$ReaderSettings.add_ValidationEventHandler({ $script:ErrorCount++ })
$Reader = [System.Xml.XmlReader]::Create($XmlFile.FullName, $ReaderSettings)
while ($Reader.Read()) { }
$Reader.Close()
# Verify the results of the XSD validation
if ($script:ErrorCount -gt 0) {
# XML is NOT valid
$false
} else {
# XML is valid
$true
}
} catch {
Write-Warning "$($MyInvocation.MyCommand.Name) - Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
}
}