How to stop a ForEach Loop?

Brass Contributor

Hi,

 

I have the following simple script that works fine, but continually loops once it has finished displaying the necessary information. Does anyone know what I need to change to get it to display the information only once?

 

import-csv .\MailboxUsersPermissions.ps1 | foreach-object {Get-Mailbox | Get-MailboxPermission | where-object { ($_.IsInherited -eq $false) -and -not ($_.User -like “NT AUTHORITY\SELF”) }}

 

I have tried using a break as shown below, but this hasn't helped.

 

ForEach ($name in Get-Content d:\PowerShell\mailboxusers.csv)
{
Get-Mailbox | Get-MailboxPermission | where-object { ($_.IsInherited -eq $false) -and -not ($_.User -like “NT AUTHORITY\SELF”) }
}
{   
break
}

 

Any help would be much appreciated.

 

Rgds

Lee

 

2 Replies

Your Get-Mailbox will be returning every mailbox in your org, and that will repeat for every $name that's in your mailbox user CSV list.

 

Looks like you should use "Get-Mailbox -Mailbox $name" so that it returns the single mailbox to pass to Get-MailboxPermission.

Thanks Paul, will give that a try this morning.