'Script stalls when using button in WPF/Powershell to connect to Exchange Online and run a foreach to create mailboxes
Let me start by saying I'm new to Powershell and WPF so bear with me. I'm trying to make something that creates resource mailboxes (via a button click) based on WPF text boxes that the user fills in. I have a button that connects to Exchange Online then a foreach should run straight after that which creates the mailboxes using a CSV imported earlier in the script. However, it seems to prompt to O365 credentials and then connects fine to Exchange Online but I can't get anything to run after that, it just stalls, even if I put something simple in place of the foreach. Can anyone shed any light on what I'm doing wrong please?
- $CSVContents is the imported CSV file with 2 columns, Name and PrimarySmtpAddress
- $Radio_Room_Mailbox is a radio button the user has to select to determine the type of resource mailbox they want to create
- $Textbox_Minimum_Duration is a textbox that may contain the minimum duration property of the mailbox
- $Textbox_Custom_Attribute is a textbox that may contain a custom attribute to be added to the mailbox
The sample below is the first of several If statements that create a room mailbox if the relevant radio button is selected and the other text boxes are empty but it never seems to get that far. I thought it may have been bad logic in the foreach but even when I remove it and just try to get something simple to run then it still doesn't work but I can no longer see the wood for the trees! Any help/thoughts/suggestions welcome and gratefully received. (Apologies for the layout of the script, it's not my strong point!)
#Create Mailbox button is clicked
$Btn_Create_Mailboxes.Add_Click({
#Connect to Exchange Online
$EXCHCRED=Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $EXCHCRED -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber -DisableNameChecking | out-host
#Create mailboxes
foreach($mailbox in $CSVContents)
{
If(($Radio_Room_Mailbox.IsChecked -eq $true) -and ($Textbox_Minimum_Duration.Text.Length -eq 0) -and ($Textbox_Custom_Attribute.Text.Length -eq 0))
{New-Mailbox -Name $mailbox.Name -PrimarySmtpAddress $mailbox.PrimarySmtpAddress -Room}
else
{
$Textbox_Display_PS_Script.Text=("Try again!")}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
