Link to home
Start Free TrialLog in
Avatar of Ackles
AcklesFlag for Switzerland

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

$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
 }
 }

Open in new window

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

do {
  $MacAddress = Read-Host -Prompt 'Enter MacAddress in Format (6 Sets) XX:XX:XX:XX:XX:XX - '
} while (!(Check-Mac $MacAddress))

Open in new window

Avatar of Ackles

ASKER

Thanks David,
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
Avatar of Ackles

ASKER

Sorry, I'm lost...
Are you saying the syntax is wrong?
Because if I give , or . , the function replaces it with :
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ackles

ASKER

Thanks a Million, David!!!