locked
Error handling in this script. RRS feed

  • Question

  • Please forgive the post as I am very new to powershell and coding. I have managed to cobble together a script that creates a mailbox based on the existing username. I now want to hand this script off to another team but need some kind of error handling so that entering a null value or a username that doesnt exist wont crash the script.

    At the moment the first part of the script is as follows.

     

    $username=read-host "Please Enter Username"

    $upn=$username + "@subdomain.com"

    $alias= 'new mailbox-'+ $username

    $database= get-mailbox $username | select-object -ExpandProperty database

    At this point if I enter a null value I get this error
    Get-Mailbox : Cannot bind parameter 'Identity'. Cannot convert value "" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". Error: "The parameter value of this type Microsoft.Exchange.Configurat
    ion.Tasks.MailboxIdParameter cannot be empty.
    Parameter name: identity"
    At C:\scripts\Finished\Create Mailbox.ps1:12 char:23
    + $database= get-mailbox <<<<  $username | select-object -ExpandProperty database
        + CategoryInfo          : InvalidArgument: (:) [Get-Mailbox], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
    And an incorrect username results in
    Get-Mailbox : The operation could not be performed because object 'someuser' could not be found on domain controller 'domaincontroller.domain.com'.
    At C:\scripts\Finished\Create Mailbox.ps1:12 char:23
    + $database= get-mailbox <<<<  $username | select-object -ExpandProperty database
        + CategoryInfo          : InvalidData: (:) [Get-Mailbox], ManagementObjectNotFoundException
        + FullyQualifiedErrorId : 75FFB5C2,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
    Could anyone kindly point me in the right direction to outputting a message to the user that they have entered either a null value or incorrect username and then starting the script again ? 
    Am I going to need Catch and Try statements ?
    Thanks for any help

    Tuesday, November 1, 2011 2:49 PM

Answers

  • $username=read-host "Please Enter Username"
    $upn=$username + "@subdomain.com"
    $alias= 'new mailbox-'+ $username
    try { 
          $database= get-mailbox $username -ea stop | select-object -ExpandProperty database
    } # end try
    catch {
        write-host "$username is invalid"
    } # end catch
    
    

    ...should work.
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Tuesday, November 1, 2011 3:11 PM
  • You can do something like below to catch empty username:

     

     

    if(-not $username){
    
    write-host -for red "Please enter a username"
    
    exit 1
    
    }
    

     

     

    You can also add a trap statement at the beginning of the script for other issues:

     

    trap {  write-host $_; continue}

    Tuesday, November 1, 2011 3:57 PM
  • Hello ExhangeAdm.  Anything for a few more points. (Only kidding)!  Here:

    do {
        $username=read-host "Please Enter Username"
        $upn=$username + "@subdomain.com"
        $alias= 'new mailbox-'+ $username
        try { 
            $database= get-mailbox $username -ea stop | select-object -ExpandProperty database
            $valid = $true
            } # end try
        catch {
            write-host "$username is invalid"
            $valid = $false
            } # end catch
        } until ($valid)
    
    


    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    Wednesday, December 21, 2011 2:54 PM

All replies

  • $username=read-host "Please Enter Username"
    $upn=$username + "@subdomain.com"
    $alias= 'new mailbox-'+ $username
    try { 
          $database= get-mailbox $username -ea stop | select-object -ExpandProperty database
    } # end try
    catch {
        write-host "$username is invalid"
    } # end catch
    
    

    ...should work.
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Tuesday, November 1, 2011 3:11 PM
  • You can do something like below to catch empty username:

     

     

    if(-not $username){
    
    write-host -for red "Please enter a username"
    
    exit 1
    
    }
    

     

     

    You can also add a trap statement at the beginning of the script for other issues:

     

    trap {  write-host $_; continue}

    Tuesday, November 1, 2011 3:57 PM
  • Sorry to resurrect this thread Bigteddy but is there a simple way I can get your solution to display username is invalid and then loop back to the $username=read-host"Please Enter Username" 

    Many thanks

    Wednesday, December 21, 2011 12:02 PM
  • Hello ExhangeAdm.  Anything for a few more points. (Only kidding)!  Here:

    do {
        $username=read-host "Please Enter Username"
        $upn=$username + "@subdomain.com"
        $alias= 'new mailbox-'+ $username
        try { 
            $database= get-mailbox $username -ea stop | select-object -ExpandProperty database
            $valid = $true
            } # end try
        catch {
            write-host "$username is invalid"
            $valid = $false
            } # end catch
        } until ($valid)
    
    


    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    Wednesday, December 21, 2011 2:54 PM
  • Fantastic ! Many thanks
    Wednesday, December 21, 2011 3:12 PM
  • My pleasure.  Have a good holiday!
    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    Wednesday, December 21, 2011 3:17 PM