Powershell help with loop

Ackles
Ackles used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
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

Author

Commented:
Thanks David,
How about the second question?
Top Expert 2016

Commented:
The way things are coded you check if the mac address is valid syntax //input validation
therefore repair-mac will never get called
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
Sorry, I'm lost...
Are you saying the syntax is wrong?
Because if I give , or . , the function replaces it with :
Top Expert 2016
Commented:
#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++

Author

Commented:
Thanks a Million, David!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial