在工作日期间,我有现场技术人员外出,他们偶尔需要将 MAC 地址添加到我们在 AD 中的无线访问组。我们不完全支持他们自己进入 AD,我们一直在使用脚本让他们以正确的方式添加 MAC 地址。 我已经承担起完全防止白痴这件事的责任,而且我几乎就在那里减去一个明显的问题。我无法阻止他们添加值大于“f”的 MAC 地址。
Write-Host "MAC Address must be entered as lowercase and without colons. EX: 14d6aa6ac9be" -ForegroundColor Yellow
$MACUserName = Read-Host -Prompt 'Please Input the MAC Address of the Device to be added to AD and press Enter'
$MACUserName = $MACUserName -replace '[\W]', ''
If ($MACUserName.Length -ne 12 -or $MACUserName -notmatch '[A-Za-z0-9]') {
Write-Host "MAC Address: " -ForegroundColor Red -NoNewline; Write-Host $MACUserName -ForegroundColor White -NoNewline; Write-Host " is not the correct length or contains invalid characters. Please verify MAC address" -ForegroundColor Red
Pause
Single-Device}
到目前为止,这是我所做的一切,显然这不仅仅是这一部分,但现在这是我住的地方。
我能够去掉任何可能输入的冒号,我的 -notmatch 部分包含所有可能的值。
如果我将 -notmatch
'[A-Za-z0-9]'
更改为 -notmatch '[A-Fa-f0-9]'
它仍然允许我添加带有 z 和诸如此类的假 MAC 地址。我该如何限制本节接受的字符?
Santiago Squarzon 的有用答案 使用 .NET API 为您的问题提供最佳解决方案。
至于你尝试了什么:
'[A-Fa-f0-9]'
匹配 one 属于指定范围的字符,这意味着输入字符串中的 one 这样的字符使表达式计算为 $true
- 即使存在这些范围之外的其他字符。
因此您必须确保构成输入字符串的所有字符都在预期范围内:
-notmatch '^[a-f0-9]+$'
或者,反转逻辑并查找至少一个 invalid 字符:
-match '[^a-f0-9]'
注:
-match
/ -notmatch
运算符默认执行substring匹配;因此,为了匹配整个字符串,需要开始和结束锚点^
和$
。
[a-f]
足以匹配 小写和大写字母,因为 -match
/ -notmatch
默认情况下不区分大小写,就像 PowerShell 通常那样。如果需要区分大小写匹配,请使用-cmatch
/
-cnotmatch
PhysicalAddress Class。您可以创建一个函数来解析用户的输入:
function Test-MacAddress {
param(
[Parameter(ValueFromPipeline)]
[string] $MacAddress
)
process {
try {
[pscustomobject]@{
ParsedMAC = [PhysicalAddress]::Parse($MacAddress.ToUpper().Replace(':', '-'))
UserInput = $MacAddress
}
}
catch {
Write-Warning 'Invalid MAC Address!'
}
}
}
Read-Host 'input mac address' | ParseMAC
工作原理示例:
PS /> Test-MacAddress 01-23-45-67-89-AB
ParsedMAC UserInput
--------- ---------
0123456789AB 01-23-45-67-89-AB
PS /> Test-MacAddress 001122334455
ParsedMAC UserInput
--------- ---------
001122334455 001122334455
PS /> Test-MacAddress f0:e1:d2:c3:b4:a5
ParsedMAC UserInput
--------- ---------
F0E1D2C3B4A5 f0:e1:d2:c3:b4:a5
PS /> Test-MacAddress 00112233445z
WARNING: Invalid MAC Address!
注意:函数中需要使用.ToUpper().Replace(':', '-')
才能兼容.NET 4.x及之前的版本. 来自
Remarks的PhysicalAddress.Parse(String)
001122334455
00-11-22-33-44-55
0011.2233.4455
00:11:22:33:44:55
F0-E1-D2-C3-B4-A5
f0-e1-d2-c3-b4-a5
参数转换属性,它处理可能的输入字符串并使其与 Windows PowerShell 5.1 以及 PowerShell 7+ 兼容,同时验证输入的 Mac 地址:
using namespace System.Management.Automation
class MacTransform : ArgumentTransformationAttribute {
[object] Transform([EngineIntrinsics] $EngineIntrinsics, [object] $InputData) {
if($InputData -isnot [string]) {
return [PhysicalAddress] $InputData
}
return [PhysicalAddress] ($InputData.ToUpper() -replace '[.:-]')
}
}
function Test-MacAddress {
param(
[Parameter(ValueFromPipeline, Mandatory)]
[MacTransform()]
[PhysicalAddress] $MacAddress
)
process {
$MacAddress
}
}
@(
001122334455
'00-11-22-33-44-55'
'00112233445z' # This one should fail
'0011.2233.4455'
'00:11:22:33:44:55'
'F0-E1-D2-C3-B4-A5'
'f0-e1-d2-c3-b4-a5'
) | Test-MacAddress