When I run PowerShell scripts for creating and modifying users to Enable-Mailbox or Disable-Mailbox, the first time the script is run I get a warning shown below. I believe that error does not always happen the next time. Here is the relevant
parts of the code I believe:
Function Load-PSSnapin {
# ----------------------------------------------------------------------------------------------
# Function to load Exchange Snap-In(s) into PowerShell
# - Checks to see if the snap-in is already loaded
# - Checks to see if the snap-in is available to be imported
# - Adds snap-in to PowerShell
# ----------------------------------------------------------------------------------------------
Param( [parameter(Mandatory=$true)][string]$Name )
if( -Not( Get-PSSnapin -Name $Name -ErrorAction:SilentlyContinue ) ) {
if( Get-PSSnapin -Registered | Where-Object { $_.Name -eq $Name } ) {
# Snap-in is available, but not loaded; Add the snap-in
Add-PSSnapin -Name $Name
} else {
# Module not available on the computer
Write-Host -ForegroundColor Red "ERROR: The specified snap-in '$name' is not found on this computer."
break
} # End if
} else {
# Module is already loaded
} # End if
} # End Function Load-Module <...other stuff...>
# Get the user's current information
$user = Get-ADUser $Change -Properties * -Server $Server
# Remove email from account
if( $user.mail.Length -gt 0 ) {
# Load the Microsoft Exchange Admin Snap-In
Load-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010
Disable-Mailbox -Identity $user.SamAccountName -DomainController $Server -Confirm:$false
}
I get the following output but it DOES enable or disable the user but gives this big'ol'warning:
WARNING: The cmdlet extension agent with the index 0 has thrown an exception in OnComplete(). The exception is:
System.InvalidOperationException: Operation is not valid due to the current state of the object.
at Microsoft.Exchange.Data.Storage.ExchangePrincipal.get_ServerFullyQualifiedDomainName()
at Microsoft.Exchange.Data.Storage.MailboxSession.Initialize(MapiStore linkedStore, LogonType logonType,
ExchangePrincipal owner, DelegateLogonUser delegateUser, Object identity, OpenMailboxSessionFlags flags,
GenericIdentity auxiliaryIdentity)
at Microsoft.Exchange.Data.Storage.MailboxSession.<>c__DisplayClass12.<CreateMailboxSession>b__10(MailboxSession
mailboxSession)
at Microsoft.Exchange.Data.Storage.MailboxSession.InternalCreateMailboxSession(LogonType logonType,
ExchangePrincipal owner, CultureInfo cultureInfo, String clientInfoString, IAccountingObject budget, Action`1
initializeMailboxSession, InitializeMailboxSessionFailure initializeMailboxSessionFailure)
at Microsoft.Exchange.Data.Storage.MailboxSession.CreateMailboxSession(LogonType logonType, ExchangePrincipal owner,
DelegateLogonUser delegateUser, Object identity, OpenMailboxSessionFlags flags, CultureInfo cultureInfo, String
clientInfoString, PropertyDefinition[] mailboxProperties, IList`1 foldersToInit, GenericIdentity auxiliaryIdentity,
IAccountingObject budget)
at Microsoft.Exchange.Data.Storage.MailboxSession.ConfigurableOpen(ExchangePrincipal mailbox, MailboxAccessInfo
accessInfo, CultureInfo cultureInfo, String clientInfoString, LogonType logonType, PropertyDefinition[]
mailboxProperties, InitializationFlags initFlags, IList`1 foldersToInit, IAccountingObject budget)
at Microsoft.Exchange.Data.Storage.MailboxSession.OpenAsSystemService(ExchangePrincipal mailboxOwner, CultureInfo
cultureInfo, String clientInfoString)
at Microsoft.Exchange.ProvisioningAgent.MailboxLoggerFactory.XsoMailer.Log(AdminLogMessageData data,
LogMessageDelegate logMessage)
at Microsoft.Exchange.ProvisioningAgent.AdminLogProvisioningHandler.OnComplete(Boolean succeeded, Exception e)
at Microsoft.Exchange.Provisioning.ProvisioningLayer.OnComplete(Task task, Boolean succeeded, Exception exception)
PS D:\PowerShell\Development>
I do NOT get this if I use the EMS directly and not just load the snappin with my script. Is there a different way to load it to make it function properly?
Find this post helpful? Does this post answer your question? Be sure to mark it appropriately to help others find answers to their searches.