Ackles
asked on
Powershell help with loop
Hello,
I need help with Powershell loop...
I have the following code, which checks the MacAddress input,
I want to put it in a loop so that if the value doesn't meet the pattern, the user should be prompted to enter again the value.
Secondly , this is a part of a longer code, how can I make the same logic on the whole code?
That means, at the end user is asked if he's fine with entries, should it continue, if not repeat the whole process....
Thanks
I need help with Powershell loop...
I have the following code, which checks the MacAddress input,
I want to put it in a loop so that if the value doesn't meet the pattern, the user should be prompted to enter again the value.
Secondly , this is a part of a longer code, how can I make the same logic on the whole code?
That means, at the end user is asked if he's fine with entries, should it continue, if not repeat the whole process....
Thanks
$MacAddress = Read-Host -Prompt "Enter MacAddress in Format (6 Sets) XX:XX:XX:XX:XX:XX - "
#Function to Validate MacAddress
#Functions
function Check-Mac ($MacAddress) {
$Value=$MacAddress -match „^[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}$“
return $Value
}
function Repair-Mac ($MacAddress) {
$Value=$MacAddress.Replace(„/“,““).Replace(„.“,““).Replace(“ „,““).Replace(„,“,““).Replace(„;“,““).Replace(„\“,““).Replace(„:“,““).Replace(„-„,““)
$Value=$Value.Insert(2,“:“).Insert(5,“:“).Insert(8,“:“).Insert(11,“:“).Insert(14,“:“)
return $Value
}
#Magic and Result
if (!(Check-Mac $MacAddress)){
$MacAddress=Repair-Mac $MacAddress
if (!(Check-Mac $MacAddress)){
Write-Host „Mac is wrong“ -ForegroundColor Red
}
else {
Write-Host „MacAddress is good“ -ForegroundColor Green
}
}
ASKER
Thanks David,
How about the second question?
How about the second question?
The way things are coded you check if the mac address is valid syntax //input validation
therefore repair-mac will never get called
therefore repair-mac will never get called
ASKER
Sorry, I'm lost...
Are you saying the syntax is wrong?
Because if I give , or . , the function replaces it with :
Are you saying the syntax is wrong?
Because if I give , or . , the function replaces it with :
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a Million, David!!!
Open in new window