Jump to content

Hide all disabled users from Global Address List


Recommended Posts

Posted

As the title says, need to hide all disabled users from GAL.

 

At the moment trying this...

 

Get-MsolUser -All | Where {$_.BlockCredential -eq $true} | Set-Mailbox -HiddenFromAddressListsEnabled $true

 

And get the following error.....

 

Set-Mailbox : The session WinRM2, 5c69bba6-adc9-4ec4-9b10-4be2398c0eb7, ps.outlook.com is not available to run

commands. The session availability is Busy.

At line:1 char:1

+ Get-MsolUser -All | Where {$_.BlockCredential -eq $true} | Set-Mailbo ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: ([PSSession]WinRM2:PSSession) [invoke-Command], InvalidRunspaceStateEx

ception

+ FullyQualifiedErrorId : InvokeCommandCommandInvalidSessionAvailability,Microsoft.PowerShell.Commands.InvokeComma

ndCommand

 

Set-Mailbox : No valid sessions were specified. Ensure you provide valid sessions that are in the Opened state and

are available to run commands.

At line:1 char:1

+ Get-MsolUser -All | Where {$_.BlockCredential -eq $true} | Set-Mailbo ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (:) [invoke-Command], PSInvalidOperationException

+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.InvokeCommandCommand

 

I get a feeling it's something simple I'm missing but not sure what...? Any assistance greatly appreciated.

Posted (edited)

Is office 365 syncing from AD, or are you strictly O365 only? I would use the following if you're synced locally from AD;

Get-ADuser -filter {enabled -eq $false} | Set-ADUser -Replace @{msExchHideFromAddresslists = $true}

You might want to specify a -searchbase for the get-aduser query

 

edit; The actual powershell error you're getting seems like a throttling issue? Probably due to running it in a pipeline, might be better to use a ForEach and loop through the results of the query

Edited by Chaniel
Posted

Trying this now....

 

Get-MsolUser -All | Where {$_.BlockCredential -eq $true} | ForEach-Object { Write-Host $_.UserPrincipalName ; Set-Mailbox -Identity $_.UserPrincipalName -HiddenFromAddressListsEnabled $True }

 

And getting the following....

 

Set-Mailbox : The session WinRM2, 5c69bba6-adc9-4ec4-9b10-4be2398c0eb7, ps.outlook.com is not available to run

commands. The session availability is Busy.

At line:1 char:111

+ ... cipalName ; Set-Mailbox -Identity $_.UserPrincipalName -HiddenFromAdd ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: ([PSSession]WinRM2:PSSession) [invoke-Command], InvalidRunspaceStateEx

ception

+ FullyQualifiedErrorId : InvokeCommandCommandInvalidSessionAvailability,Microsoft.PowerShell.Commands.InvokeComma

ndCommand

 

Get-MsolUser : No valid sessions were specified. Ensure you provide valid sessions that are in the Opened state and

are available to run commands.

At line:1 char:1

+ Get-MsolUser -All | Where {$_.BlockCredential -eq $true} | ForEach-Ob ...

+ ~~~~~~~~~~~~~~~~~

+ CategoryInfo : NotSpecified: (Microsoft.Onlin...omation.GetUser:GetUser) [Get-MsolUser], CmdletInvocati

onException

+ FullyQualifiedErrorId : System.Management.Automation.CmdletInvocationException: No valid sessions were specified

. Ensure you provide valid sessions that are in the Opened state and are available to run commands. ---> System.M

anagement.Automation.PSInvalidOperationException: No valid sessions were specified. Ensure you provide valid sess

ions that are in the Opened state and are available to run commands.

at Microsoft.PowerShell.Commands.InvokeCommandCommand.BeginProcessing()

at System.Management.Automation.Cmdlet.DoBeginProcessing()

at System.Management.Automation.CommandProcessorBase.DoBegin()

--- End of inner exception stack trace ---

at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Excepti

on exception)

at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)

at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)

at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)

at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolea

n createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingB

ehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Obje

ct[] args)

at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.b__0()

at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action acti

on)

at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHa

ndlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInf

o, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)

at System.Management.Automation.ScriptBlock.InvokeUsingCmdlet(Cmdlet contextCmdlet, Boolean useLocalScope, Erro

rHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)

at Microsoft.PowerShell.Commands.ForEachObjectCommand.ProcessRecord()

at System.Management.Automation.CommandProcessor.ProcessRecord()

at System.Management.Automation.CommandProcessorBase.DoExecute()

at System.Management.Automation.Internal.Pipe.AddToPipe(Object obj)

at System.Security.SecurityContext.Run(SecurityContext securityContext, ContextCallback callback, Object state)

 

at System.Management.Automation.MshCommandRuntime.WriteObject(Object sendToPipeline)

at System.Management.Automation.Cmdlet.WriteObject(Object sendToPipeline)

at Microsoft.PowerShell.Commands.WhereObjectCommand.ProcessRecord()

at System.Management.Automation.CommandProcessor.ProcessRecord()

at System.Management.Automation.CommandProcessorBase.DoExecute()

at System.Management.Automation.Internal.Pipe.AddToPipe(Object obj)

at System.Management.Automation.Internal.Pipe.AddItems(Object objects)

at System.Security.SecurityContext.Run(SecurityContext securityContext, ContextCallback callback, Object state)

 

at System.Management.Automation.MshCommandRuntime.WriteObject(Object sendToPipeline, Boolean enumerateCollectio

n)

at System.Management.Automation.Cmdlet.WriteObject(Object sendToPipeline, Boolean enumerateCollection)

at Microsoft.Online.Administration.Automation.MsolCmdlet.ProcessList(SearchDefinition searchDefinition, Int32 m

axResultsSize) in X:\bt\1067178\repo\src\dev\PowerShell.V1\modules\psmodule\Cmdlets\MsolCmdlet.cs:line 372

at Microsoft.Online.Administration.Automation.GetUser.ProcessRecordAll() in X:\bt\1067178\repo\src\dev\PowerShe

ll.V1\modules\psmodule\Cmdlets\GeneratedCmdlets.cs:line 9325,Microsoft.Online.Administration.Automation.GetUser

Posted

Since you're piping it, it's still going to run in parallel and likely hit whatever throttling MS is doing on their servers.

 

This should work...

 

$disabledusers = [color=#333333][i]Get-MsolUser -All | Where {$_.BlockCredential -eq $true} 
ForEach ($user in $disabledusers) {Set-Mailbox -Identity $user.UserPrincipalName -HiddenFromAddressListsEnabled $True }[/i][/color]

  • Thanks 1
Posted
Since you're piping it, it's still going to run in parallel and likely hit whatever throttling MS is doing on their servers.

 

This should work...

 

$disabledusers = [color=#333333][i]Get-MsolUser -All | Where {$_.BlockCredential -eq $true} 
ForEach ($user in $disabledusers) {Set-Mailbox -Identity $user.UserPrincipalName -HiddenFromAddressListsEnabled $True }[/i][/color]

 

Thanks for that. It didn't work at first, errored but then logged out of the session, back in and it worked.

 

Thanks again Chaniel

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...