try/catch in jobs using job tracker

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
SakJaTeda
Posts: 11
Last visit: Fri Jan 20, 2023 2:53 pm

try/catch in jobs using job tracker

Post by SakJaTeda »

Hi guys,

I am running into issues with some code I am trying to write. It is a GUI application that is used to create mailboxes in Exchange. I had it working but seeing as remoting takes a while, it locks the GUI which is why I decided to try using the job tracker functionality instead and achieve a responsive application. It does create the mailbox but it does not return what I want it to return - which is just a simple string that I would then append to a text box in the -CompletedScript part. So being new to jobs and GUI stuff in general, I think I am doing something wrong. Any advise would be greatly appreciated.
Last edited by SakJaTeda on Thu May 24, 2018 6:47 am, edited 1 time in total.
User avatar
SakJaTeda
Posts: 11
Last visit: Fri Jan 20, 2023 2:53 pm

Re: try/catch in jobs using job tracker

Post by SakJaTeda »

I'm thinking I should have included the whole function, so here goes (sorry if the formating is off a little bit - copy/pasted directly from PSStudio):
Attachments
function.ps1
(4.13 KiB) Downloaded 105 times
Last edited by SakJaTeda on Thu May 24, 2018 6:49 am, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: try/catch in jobs using job tracker

Post by jvierra »

Hello SakJaTeda,

To make it easier to read your code, can you please edit your posts and remove the large block of code and post it as an attachment.
User avatar
SakJaTeda
Posts: 11
Last visit: Fri Jan 20, 2023 2:53 pm

Re: try/catch in jobs using job tracker

Post by SakJaTeda »

Sorry about that - done as requested.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: try/catch in jobs using job tracker

Post by jvierra »

Thank you. Now that I can read it.

This line retrieves an object.
$results = Receive-Job -Job $job

In debugger inspect the object to see what it contains.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: try/catch in jobs using job tracker

Post by jvierra »

Also add a return value at the very end of the script block to detect a fall through. If the code fals through you will get no resilts.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: try/catch in jobs using job tracker

Post by jvierra »

There is no need to remove the session as it will be removed when the script block completes.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: try/catch in jobs using job tracker

Post by jvierra »

Here is a simpler and cleaner way to do this:
Attachments
function2.ps1
(1.09 KiB) Downloaded 100 times
User avatar
SakJaTeda
Posts: 11
Last visit: Fri Jan 20, 2023 2:53 pm

Re: try/catch in jobs using job tracker

Post by SakJaTeda »

Thanks for all the input jvierra, I managed to make it work now.

A little FYI perhaps - you do need the $Global:ErrorActionPreference = 'Stop' the way I had it because the errors are not being caught by try/catch otherwise (in these specific cases anyway). Adding the -ErrorAction Stop is not sufficient for some reason. I would reference the source where I read it but can't really find it right now.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: try/catch in jobs using job tracker

Post by jvierra »

No. I think you are not understanding how this works. The only spot that is missing the parameter is the connect so maybe that would make you think that. The Try/Catch would work better if it was embedded. THis would allow only one point of return which is more in keeping with programming standards.

This would be a preferred way to do this. One point of return.

Code: Select all

$jobscript = {
    param ($UID,
        $Database,
        $Shared,
        $Credentials
    )
    $results = 'FALL THROUGH'
    $ErrorActionPreference = 'Stop'
    try{
        $ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri '<redacted>' -Authentication Kerberos -Credential $Credentials
        Import-PSSession $ExchangeSession | Out-Null
        if($mb){
            $results = "The mailbox $UID is already enabled."
        }else{
            if ($Shared){
                Enable-Mailbox -Identity $UID -Database $Database -Shared
                $results = "Shared mailbox $UID successfully enabled."
            }else{
                Enable-Mailbox -Identity $UID -Database $Database
                $results = "Mailbox $UID successfully enabled."
            }
        }
    }
    catch{
        $results = "There was an issue with creating the mailbox: $_"
    }
    $results
}
Multiple Try/Catch blocks increase the chances of unforeseen fall through and execute code that should not be executed after the previous exception.
If you have it working then don't worry about it for now.
This topic is 5 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked