Help with format enforcement

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Mark Prior Mark Prior 2 months, 2 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #46583
    Profile photo of Mark Prior
    Mark Prior
    Participant

    Hi all

    i have the below script, all works well however i would like to enforce the formatting of the data the user is allowed to enter for the lines

    $datefrom = Read-Host "Enter Start date 'DD/MM/YYYY'"
    $dateto = Read-Host "Enter End date 'DD/MM/YYYY'"

    i would like to enforce numbers only and in the date format DD/MM/YYY, greatful for any help

    cls
    Write-Host
    Write-Host
    write-host "All items will be restored to 'EMEA-Restoreditems' Mailbox" -foregroundcolor "yellow" -backgroundcolor "black"
    Write-Host
    write-host
    
    
    ##################
    #### Varibles ####
    ##################
    
    $user = Read-Host "Enter Username"
    $datefrom = Read-Host "Enter Start date 'DD/MM/YYYY'"
    $dateto = Read-Host "Enter End date 'DD/MM/YYYY'"
    
    ################################
    #### Sent / Recieved Choice ####
    ################################
    
    $title = "Choose Send / Receive"
    $message = "Do you want restore Sent or Received Items"
    
    $received = New-Object System.Management.Automation.Host.ChoiceDescription "&Received"
    
    $Sent = New-Object System.Management.Automation.Host.ChoiceDescription "&Sent"
    
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($Received, $Sent)
    
    $result = $host.ui.PromptForChoice($title, $message, $options, 0) 
    
    switch ($result)
        {
            0 {$choice = "Received:"}
            1 {$choice = "Sent:"}
        }
    
    #################
    #### Command ####
    #################
    
    Search-Mailbox $user -SearchDumpsterOnly -TargetMailbox emea-restoreditems -SearchQuery $Choice"$datefrom..$dateto" -TargetFolder Inbox
    
    
    
    • This topic was modified 2 months, 2 weeks ago by Profile photo of Mark Prior Mark Prior.
    #46597
    Profile photo of Craig Duff
    Craig Duff
    Participant
    Do {
        $datefrom = Read-Host "Enter Start date 'DD/MM/YYYY'"
        $dateobj = $datefrom -as [datetime]
        If(-not $dateobj) {
            Write-Warning "Start Date is Invalid, Please enter in the format of 'DD/MM/YYYY'"
        }
    } While ( -not $dateobj )

    There is one way which relies on date casting, meaning any valid date format would work. If you'd consider making a parameterized function out of the whole thing you can make the input mandatory and valid by this Param declaration:

    Function MyScript {
        Param(
            [Parameter(
                Mandatory = $True
            )]
            [DateTime]
            $DateFrom
        )
    }
    #46607
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    "Enforcement" is tough using Read-Host. The first question is who is your audience. If it's a tech person, you can use loops. If it's an end user, you really should use a GUI interface where you can actually use calendar controls, but this would be easier to build with something like Sapien's Powershell Studio.

    Take a look at this loop which will loop continually unless a valid date or "x" is entered at the prompt:

    while ($true) {
      $theDate = Read-Host "Provide a start date (type x to exit)"
      if ((($theDate -as [DateTime]) -ne $null)) {
        "You chose {0}" -f (Get-Date $theDate)
        break
      }
      elseif ($theDate -eq "x") {
        Write-Host –Fore "Yellow" "Exiting loop."
        break
      } 
      else {
        Write-Host –Fore "Red" "Please provide a valid date."
      }
    } 
    
    #46695
    Profile photo of Mark Prior
    Mark Prior
    Participant

    many thanks to both its great to see more than one way to acomplish this, i was going to use params and may in the future but restoring from the dumpster is new for my teams, previously it was netapp so decided to prompt the correct choices to be a bit more user friendly,

    anyway current version if anyone else is after a similar solution

    #################################
    #### Dumpster Restore Script ####
    #################################
    #### CreatedBy: Mark.Prior ######
    #################################
    
    cls
    Write-Host
    Write-Host
    write-host "All items will be restored to 'EMEA-Restoreditems' Mailbox" -foregroundcolor "yellow" -backgroundcolor "black"
    Write-Host
    write-host
    
    
    ##################
    #### Varibles ####
    ##################
    
    $user = Read-Host "Enter Alias"
    Write-Host –Fore "Yellow" "Working with Mailbox: $user"
    write-host
    
    ###################
    #### Date From ####
    ###################
    
    while ($true) {
      $Datefrom = Read-Host "Provide a Start Date (DD/MM/YYY) (type x to exit)"
      if ((($Datefrom -as [DateTime]) -ne $null)) {
      
        write-host "You chose $datefrom" -foregroundcolor "yellow" -backgroundcolor "black"
        Write-host
        break
      }
      elseif ($Datefrom -eq "x") {
        Write-Host –Fore "Yellow" "Exiting Restore Script."
        exit
      } 
      else {
        Write-Host –Fore "Red" "Please provide a valid date."
      }
    } 
    Write-Host
    
    
    #################
    #### Date To ####
    #################
    
    while ($true) {
      $Dateto = Read-Host "Provide an End date (DD/MM/YYY) (type x to exit)"
      if ((($Dateto -as [DateTime]) -ne $null)) {
        write-host "You chose $dateto" -foregroundcolor "yellow" -backgroundcolor "black"
        Write-host
        break
      }
      elseif ($Dateto -eq "x") {
        Write-Host –Fore "Yellow" "Exiting Restore Script."
        exit
      } 
      else {
        Write-Host –Fore "Red" "Please provide a valid date."
      }
    } 
    
    
    ################################
    #### Sent / Recieved Choice ####
    ################################
    
    $title = "Choose Send / Receive"
    $message = "Do you want restore Sent or Received Items"
    
    $received = New-Object System.Management.Automation.Host.ChoiceDescription "&Received"
    
    $Sent = New-Object System.Management.Automation.Host.ChoiceDescription "&Sent"
    
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($Received, $Sent)
    
    $result = $host.ui.PromptForChoice($title, $message, $options, 0) 
    
    switch ($result)
        {
            0 {$choice = "Received:"}
            1 {$choice = "Sent:"}
        }
    
    #################
    #### Command ####
    #################
    
    Search-Mailbox $user -SearchDumpsterOnly -TargetMailbox emea-restoreditems -SearchQuery $Choice"$datefrom..$dateto" -TargetFolder Inbox
    
    Write-Host
    write-host "Any Items listed have been restored to 'EMEA-Restoreditems' Mailbox under the Inbox folder" -foregroundcolor "yellow" -backgroundcolor "black"
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.