Aujourd’hui je vous propose ma fonction de vérification des GUID.
Elle vérifie:
- Si le GUID contient bien 5 blocs séparés par un tiret.
- Si le nombre de caractères par bloc est correct (8-4-4-4-12).
- Si les caractères sont bien hexadécimaux.
Si il est valide, le GUID en majuscule est injecté dans le presse-papiers.
PowerShell
function Approve-BIOSGUID {
[CmdletBinding()]
param (
[Parameter(
Mandatory=$true,
Position=0
)]
[ValidateNotNullOrEmpty()]
[string]
$GUID
)
if($GUID -notmatch "^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$") {
$GUIDSPlit = $GUID -split "-"
if($GUIDSPlit.Count -ne 5) {
"ERROR: This GUID is invalid, there must be 5 blocks divided by ""-"" symbol. Hexadecimal 8-4-4-4-12."
}
else{
$IDBlockForbiden = @()
if($GUIDSPlit[0] -notmatch "^[A-Fa-f0-9]+$"){ $IDBlockForbiden += 1 }
if($GUIDSPlit[1] -notmatch "^[A-Fa-f0-9]+$"){ $IDBlockForbiden += 2 }
if($GUIDSPlit[2] -notmatch "^[A-Fa-f0-9]+$"){ $IDBlockForbiden += 3 }
if($GUIDSPlit[3] -notmatch "^[A-Fa-f0-9]+$"){ $IDBlockForbiden += 4 }
if($GUIDSPlit[4] -notmatch "^[A-Fa-f0-9]+$"){ $IDBlockForbiden += 5 }
if($IDBlockForbiden.Count -eq 1) {
"ERROR: block $($IDBlockForbiden[0]) contain an invalid character (only hexadecimal allowed)."
}
elseif($IDBlockForbiden.Count -gt 1){
$join = @()
$jointext = $null
$join += $IDBlockForbiden[0..$($IDBlockForbiden.Count-2)] -join ", "
$join += $IDBlockForbiden[$($IDBlockForbiden.Count-1)]
$jointext = $join -join " & "
"ERROR: blocks $jointext contains invalid characters (only hexadecimal allowed)."
}
$IDBlockWrongCount = @()
if($GUIDSPlit[0].Length -ne 8) { $IDBlockWrongCount += 1 }
if($GUIDSPlit[1].Length -ne 4) { $IDBlockWrongCount += 2 }
if($GUIDSPlit[2].Length -ne 4) { $IDBlockWrongCount += 3 }
if($GUIDSPlit[3].Length -ne 4) { $IDBlockWrongCount += 4 }
if($GUIDSPlit[4].Length -ne 12){ $IDBlockWrongCount += 5 }
if($IDBlockWrongCount.Count -eq 1) {
"ERROR: block $($IDBlockWrongCount[0]) size is incorrect (8-4-4-4-12)."
}
elseif($IDBlockWrongCount.Count -gt 1){
$join = @()
$jointext = $null
$join += $IDBlockWrongCount[0..$($IDBlockWrongCount.Count-2)] -join ", "
$join += $IDBlockWrongCount[$($IDBlockWrongCount.Count-1)]
$jointext = $join -join " & "
"ERROR: blocks $jointext size are incorrect (8-4-4-4-12)."
}
}
}
else {
"GUID is valid."
$GUID.ToUpper() | Clip.exe
}
}
0 commentaire