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

LVL 11
AcklesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
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

0
AcklesAuthor Commented:
Thanks David,
How about the second question?
0
David Johnson, CD, MVPOwnerCommented:
The way things are coded you check if the mac address is valid syntax //input validation
therefore repair-mac will never get called
0
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

AcklesAuthor Commented:
Sorry, I'm lost...
Are you saying the syntax is wrong?
Because if I give , or . , the function replaces it with :
0
David Johnson, CD, MVPOwnerCommented:
#Function to Validate MacAddress
#Functions
 function Check-Mac {
   <#
      .SYNOPSIS
      Describe purpose of "Check-Mac" in 1-2 sentences.

      .DESCRIPTION
      Add a more complete description of what the function does.

      .PARAMETER MacAddress
      Describe parameter -MacAddress.

      .EXAMPLE
      Check-Mac -MacAddress Value
      Describe what this call does

      .NOTES
      Place additional notes here.

      .LINK
      URLs to related sites
      The first link is opened by Get-Help -Online Check-Mac

      .INPUTS
      List of input types that are accepted by this function.

      .OUTPUTS
      List of output types produced by this function.
   #>


 
   param
   (
     [Parameter(Mandatory=$true,HelpMessage='Add help message for user')]
     [Object]$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 {
   <#
      .SYNOPSIS
      Describe purpose of "Repair-Mac" in 1-2 sentences.

      .DESCRIPTION
      Add a more complete description of what the function does.

      .PARAMETER MacAddress
      Describe parameter -MacAddress.

      .EXAMPLE
      Repair-Mac -MacAddress Value
      Describe what this call does

      .NOTES
      Place additional notes here.

      .LINK
      URLs to related sites
      The first link is opened by Get-Help -Online Repair-Mac

      .INPUTS
      List of input types that are accepted by this function.

      .OUTPUTS
      List of output types produced by this function.
   #>
  [CmdletBinding()]
   param
   (
     [Object]$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
 do {
   $MacAddress = Read-Host -Prompt 'Enter MacAddress in Format (6 Sets) XX:XX:XX:XX:XX:XX - '
   if (!(Check-Mac -MacAddress $MacAddress)){
     $MacAddress=Repair-Mac -MacAddress $MacAddress
 
     if (!(Check-Mac -MacAddress $MacAddress)){
       Write-Verbose -Message 'Mac is wrong'
     }
 
     else {
       Write-Verbose -Message 'MacAddress is good'
     }
   } 
 }while (!(Check-Mac $MacAddress))
 $MacAddress

Open in new window

now you can do your stuff i.e. ask if it is correct and so forth++
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AcklesAuthor Commented:
Thanks a Million, David!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.