2016-05-05 48 views
1
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 
    } 
} 

這總是返回找不到得到錯誤而if語句Powershell的

回答

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 
+0

感謝嘗試了很多!如果(([String] $ checkvariable).length -eq 12 -And $ checkvariable -match'這個函數IsValidMacAddress($ checkvariable) { }^[0-9A-FA-F] + $'){ 寫 - 輸出 「有效MAC」 回報1 } 其他 { 寫輸出 「未找到」 返回0 }} –