在if语句Powershell中测试两个条件时出错

时间:2016-05-05 01:36:51

标签: powershell

function IsValidMacAddress($checkvariable)
{
  Write-Output "checking mac"
  if(($checkvariable -match '^[0-9a-fA-F]+$') -and ($checkvariable.length -eq 12)) 
  {
    Write-Output "valid mac"
    return 1
  }
  else
  {
    Write-Output "not found"
    return 0
  }
}

这总是返回未找到

1 个答案:

答案 0 :(得分:0)

只是略有不同,但试试这个

function IsValidMacAddress ([string]$checkvariable) {
    $checkvariable = $checkvariable -replace '-|:'
    Write-Host "checking mac $checkvariable"
    if ($checkvariable -match '^[0-9a-f]{12}$') {
        Write-Host "$checkvariable is valid mac"
        return 1
    } else {
        Write-Host "$checkvariable is not valid mac"
        return 0
    }
}

IsValidMacAddress de:ad:be:ef:ca:fe
IsValidMacAddress de-ad-be-ef-ca-fe
IsValidMacAddress dead-beef-cafe
IsValidMacAddress deadbeefcafe