Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

How do I arrange mutilple foreach loops into a single loop for each item?

$
0
0

I have written this script to find out who has access to an Exchange Distribution list.  They are locked down so staff can’t spam the whole business with their latest request to sponsor them for running, walking, falling etc. 

What I want the script to do is run as one block running each item all the way through the loop before it picks the next item in the CSV file.  At the moment it runs a separate processes for each line

So I want it to run like this (If it makes sense):

For object 1 do A,B,C,D

For Object 2 do A,B,C,D

For Object 3 do A,B,C,D

 

Instead of:

For Object 1 do A, For Object 2 do A, For Object 3 do A

For Object1 do B, For Object 2 do B, For Object 3 do B

For Object1 do C, For Object 2 do C, For Object 3 do C

For Object1 do D, For Object 2 do D, For Object 3 do D

 

#Install Exchange Snapin

Add-PSSnapinMicrosoft.Exchange.Management.PowerShell.E2010

 

#Pull list of Disributiion lists (DL) to expand. Will be Import-csv in final script

$List="allccdepusers"

 

#Make a text file to record this info

#Pull list of users and groups that can "Send As" to this DL

$List|ForEach-Object{

$group=$List

New-Itemc:\DistroGroups\$Group.txt-typefile-force

$Dist=Get-DistributionGroup$Group|Select-Object-ExpandpropertyAcceptMessagesOnlyFromSendersOrMembers

}

 

#If the object is a group expand out the group and list each member along with a line that tells what group they are in

$Dist|Where-Object {$_.Parent -like"my.domain/CC/Groups*"} |ForEach-Object {

Add-Contentc:\DistroGroups\$Group.txt"Because they are in this AD Group: $_.distinguishedName"

Get-ADgroupmember-identity$_.distinguishedName -Recursive|Select-Object-ExpandPropertyName|  Add-Contentc:\DistroGroups\$Group.txt

}

 

#If the object is a shared mailbox list what the shared mailbox is and who can "Send As" that, but filter out all the system stuff as I only want people and where they are

$Dist|Where-Object {$_.Parent -eq"my.domain/CC/Users/SharedMailboxes"} |ForEach-Object {

Add-Contentc:\DistroGroups\$Group.txt"Because they can Send As to this mailbox, which can Send As $group"

Get-ADPermission-Identity$_.distinguishedName  |Where-Object {($_.Deny -like"False") -and ($_.User -notlike"CC\mobil*") -and ($_.User -notlike"cc\ser_*") -and ($_.User -notlike"CC\Exchange Windows Permissions") -and ($_.User -notlike  "CC\MRE-*") -and ($_.User -like"cc\*") -and ($_.AccessRights -Like"ExtendedRight")} |ForEach-Object {

 $Sam=Split-Path$_.User -Leaf

 Get-ADUser$Sam|Select-Object-ExpandPropertyName|Add-Contentc:\DistroGroups\$Group.txt

 }

 }

 

#Get the list of users that explicitly granted "Send As" to this DL

Add-Contentc:\DistroGroups\$Group.txt"Listed Explicitly"

$Dist|Where-Object {$_.Parent -like"my.domain/CC/Users/Staff*"} |ForEach-Object {

Add-Contentc:\DistroGroups\$Group.txt"$_.Name"

}

 

 

 

 


Viewing all articles
Browse latest Browse all 8583

Trending Articles