April 28, 2015 at 5:12 pm #24817
I'm trying to get some error handling in my scripts and have them stop executing when an error is hit. But seems with these O365 commands it works in the ISE but now when I run from the console. Any help/direction would be appreciated.
# Connecting to the cloud
$Creds = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Creds -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
Connect-MSOLService -credential $creds
# Connecting to the cloud
$ErrorActionPreference = "Stop"
# Getting mailbox that permissions need to be modified ON
Write-Host -ForegroundColor Green "Enter the email address of the mailbox you want permissions to be granted to or 'q' to quit: " -NoNewline
$Mailbox = Read-Host
If ($mailbox -ne 'q')
# Getting the user that needs access to the mailbox
Write-Host -ForegroundColor Green "Enter the email address of the user that you want to give permissions to that mailbox: " -NoNewline
$User = Read-Host
# Setting the Permissions to the mailbox.
Add-MailboxPermission $Mailbox -User $User -AccessRights FullAccess -InheritanceType All -ErrorAction Stop
# Allowing option to set Send As Permissions
Write-Host -ForegroundColor Green "Do you want the user to have right to send as the users mailbox as well? Y/N: " -NoNewline
$SendAsAccess = Read-Host
If ($SendAsAccess -eq 'Y')
Add-RecipientPermission $Mailbox -Trustee $User -AccessRights SendAs -ErrorAction Stop
# Getting details to verify results
$results = Get-MailboxPermission $Mailbox | Select User, AccessRights
# Writing results to screen
Write-Host -ForegroundColor Magenta "These are the users that have access to this mailbox and their access level: " -NoNewline
} Until ($mailbox -eq "q")
Write-Host "Operation Failed. Find the Error Message below:" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
#Reset the Error Action to Default
$ErrorActionPreference = "Continue"
April 28, 2015 at 5:50 pm #24819
So... in general, I'm not sure setting $ErrorActionPreference is a fabulous idea. Ideally, if you're only running cmdlets, you want to use -ErrorAction on each one that you plan to catch errors for. See, "The Big Book of PowerShell Error Handling," our free ebook.
That's a good-sized script, and obviously a little specific to what you're doing, so I can't really run it to see what's happening. Can you be more specific about what's working in the ISE, but not in the Console? And you've confirmed this is true even for a freshly-run ISE?
April 28, 2015 at 7:02 pm #24821
Sorry sometimes you forget the things you write may not make sense to other people. But basically what this does is connects to the O365 enviroment, Prompts a user for the email address of a user and the email address of a person that should have access to the first account.
Once it get's that it sets those permissions. Then asks if send as permissions should be granted as well.
Ideally if either of the email addresses are not valid, or some other error occurs with the granting permissions line then the script should stop and not get to the send as permissions part of the script. What happens though is it keeps going through the rest of the script.
I just tried it on a server I've got setup to host scripts as XenApps for our helpdesk and it worked fine so there must be something I've done to my console to make the error handeling fail. I'm definatly going to check out the ebook though.
You must be logged in to reply to this topic.