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.
try/catch in jobs using job tracker
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.
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.
try/catch in jobs using job tracker
Last edited by SakJaTeda on Thu May 24, 2018 6:47 am, edited 1 time in total.
Re: try/catch in jobs using job tracker
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 106 times
Last edited by SakJaTeda on Thu May 24, 2018 6:49 am, edited 1 time in total.
Re: try/catch in jobs using job tracker
Here is a simpler and cleaner way to do this:
- Attachments
-
- function2.ps1
- (1.09 KiB) Downloaded 100 times
Re: try/catch in jobs using job tracker
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.
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.
Re: try/catch in jobs using job tracker
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.
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 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
}
If you have it working then don't worry about it for now.