Make sure a variable contains only 3 digits each from 0-9

This topic contains 3 replies, has 1 voice, and was last updated by Profile photo of Graham Beer Graham Beer 1 year, 9 months ago.

  • Author
    Posts
  • #26818
    Profile photo of Graham Beer
    Graham Beer
    Participant

    I'm after some help (again) I'm getting a text file updated with a port number by a gui box for the end user. I only want a max of 3 digits in totally and each one can only range from 0-9. What can i do ? the \d says its a digit ...got that far !

  • #26819
    Profile photo of Graham Beer
    Graham Beer
    Participant

    This looks like it might work...

    "^\d{3}$"

  • #26820
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Cracked it !!!

    do {
    $AAHelpDirectory = 'C:\AAHelp\tmp\port.cfg'
    $CurrentPort = get-content "C:\AAHelp\tmp\port.cfg"
    [int]$NewPortNumber = [Microsoft.VisualBasic.Interaction]::InputBox("`nPort is currently set to $CurrentPort `n`n`n`n`nPlease Enter the new port number", "AAHELP PORT MANAGER") 
       }
    Until ($NewPortNumber -match "^\d{3}$")
    
  • #26823
    Profile photo of Graham Beer
    Graham Beer
    Participant

    The final script looked like this, if your interested ! 🙂

     
     
    #Add .NET assembly for GUI box  
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
    
    #Run 
    do {
    #Set AAHelp port.cfg location
    $AAHelpDirectory = 'C:\AAHelp\tmp\port.cfg'
    #Check current port number
    $CurrentPort = get-content "C:\AAHelp\tmp\port.cfg"
    #Run GUI box and request new 3 digit number
    [int]$NewPortNumber = [Microsoft.VisualBasic.Interaction]::InputBox("`nPort is currently set to $CurrentPort `n`n`n`nPlease Enter the new port number `n(ONLY 3 Digits)", "AAHELP PORT MANAGER") 
       }
    #Loop until 3 digit number is supplied
    Until ($NewPortNumber -match "^\d{3}$")
    
    #Update port.cfg file
    Set-Content -Path $AAHelpDirectory -Value $NewPortNumber
    

You must be logged in to reply to this topic.