使用 powershell 提取和打印 MAC 的前 3 个八位字节

Extracting and printing the first 3 octets of a MAC using powershell

if($ping)
    {
        ( $mac | ? { $_ -match $ip } ) -match "([0-9A-F]{2}([:-][0-9A-F]{2}){5})" | out-null;
        if ( $matches ) {
            Select-String -Pattern '[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}' -Path $matches[0] |
            ForEach-Object {$_.Matches[0].Value}
        } else {
            "Not Found"
        }
    }

是我目前使用的。不确定我是否在 PowerShell 中正确执行此操作。

我正在尝试提取 MAC 的前 3 个 XX:XX:XX,以便我可以将其用于比较。我只是不确定赛后如何拉。

您可以使用Remove裁剪前8个字符:

$mac ='00:0a:95:9d:68:16' 

$mac.Remove(8, ($mac.Length -8))

输出:

00:0a:95

您也可以使用正则表达式来完成:

[regex]::Match($mac, '^(.{8})').Groups[1].Value