Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Exchange Powershell looping - import csv

Posted on 2012-08-21
6
Medium Priority
?
866 Views
Last Modified: 2012-08-21
Trying to put a script together where I can put a list of people into a CSV file (like 1 column), and then run a script that for everyone in the list, it will add full mailbox permissions as well as Send As permissions.

Here's what I got but it appears to ignore the users that are in the CSV file and proceeds to try to run the script for everyone in the Exchange database instead:

$Thelist = Import-csv "H:\LoopList.csv"
ForEach ($theobject in $thelist)
{
	foreach ($theobject2 in $thelist)
	{
		$theMBDN = (Get-Mailbox $theobject2.themailbox).distinguishedname
		Add-ADPermission $thembDN -Extendedrights “Send As” -User $theobject.theuser
		Add-MailboxPermission $thembDN -Accessrights “FullAccess” -User $theobject.theuser
	}
}

Open in new window



Something I am missing here?
0
Comment
Question by:garryshape
  • 3
  • 2
6 Comments
 
LVL 11

Assisted Solution

by:netballi
netballi earned 800 total points
ID: 38317065
Try using the txt file as input for your looplist

$path1 = "H:\LoopList.txt"
$thelist = @(Get-Content $path1)
Foreach ($theobject2 in $thelist)
      {
            then your loop
      }
0
 
LVL 9

Accepted Solution

by:
chrismerritt earned 1200 total points
ID: 38317214
Assuming a CSV file like this:

User
User1@email.com
User2@email.com
User3@email.com

You should be able to do this as follows, remove -WhatIf if you're happy with testing. Don't see why you need to get Mailbox etc, should work anyway!

$UserList = Import-Csv "C:\Temp\Looplist.csv"
foreach ($Line in $UserList)
{
	Write-Host "Processing User $($Line.User)..."
	foreach ($PermissionLine in $UserList)
	{
		Write-Host "Granting $($PermissionLine.User) Send AS over $($Line.User)"
		Add-ADPermission $Line.User -Extendedrights "Send As" -User $PermissionLine.User -WhatIf
		Write-Host "Granting $($PermissionLine.User) Full Permissions over $($Line.User)"
		Add-MailboxPermission $Line.User -Accessrights "FullAccess" -User $PermissionLine.User -WhatIf
	}
}

Open in new window

0
 

Author Comment

by:garryshape
ID: 38317359
Wouldn't this be a better way?
Or is there redundancy in this way?

$path = "H:\LoopList.txt"
$thelist = @(Get-Content $path)
Foreach ($name in $thelist)
{
Foreach ($name2 in $thelist)
{

write-host $name
Add-ADPermission $name -Extendedrights “Send As” -User $name2
Add-MailboxPermission $name -Accessrights “FullAccess” -User $name2
}
}

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 9

Expert Comment

by:chrismerritt
ID: 38317372
Your way would work as well I assume, it's effectively the same thing. More than one way to skin a cat with PowerShell.
0
 

Author Closing Comment

by:garryshape
ID: 38317770
Thanks, it's the nested foreach that I was trying to figure out. Still not sure how it works when you use the same variable name but add a "2" to it, but I guess if it works without breaking anything I'm happy.
0
 
LVL 9

Expert Comment

by:chrismerritt
ID: 38317949
When you have a list of items, you can declare an entirely new variable during the for each stage. This variable then becomes each element of your list in turn until the end of the loop.

For example copy/paste this into PowerShell:

$List = "Item1","Item2"
foreach ($Value in $List)
{
      Write-Host $Value
}

If you then type $Value it will return the last value from the list of items ("Item2") why? because it's the last element it processed. PowerShell stores variables in memory on your current session which can be really useful too.
0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes Top 9 Exchange troubleshooting utilities that every Exchange Administrator should know. Most of the utilities are available free of cost. List of tools that I am going to explain in this article are:   Microsoft Remote Con…
Steps to fix error: “Couldn’t mount the database that you specified. Specified database: HU-DB; Error code: An Active Manager operation fail”
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question