Get answers from your peers along with millions of IT pros who visit Spiceworks.
Join Now

Can someone help me with a script I've been trying to craft?

I'm trying to get send as permissions for users in a csv file. Below is the script I'm trying to fix since it doesn't seem to work:

Powershell
$credential = New-Object System.Management.Automation.PsCredential("username",$password)

import-module activedirectory

$exchangeserver = "Exchangeservername"

#$E2K16 = new-pssession -configurationname Microsoft.Exchange -connectionuri "http://$exchangeserver/PowerShell" -authentication kerberos -credential $credential
$E2K16 = new-pssession -configurationname Microsoft.Exchange -connectionuri "http://$exchangeserver/PowerShell" -authentication kerberos 
import-pssession $E2K16 -allowclobber -disablenamechecking:$True 2>&1 | out-null
connect-PSSession $E2K16
Enter-PSSession $E2K16

###ALWAYS UPDATE THE PATH###################
$csv = import-csv "\\sserver04\c$\EXO\batches\Batch000.csv"
$Mailboxes = Get-Mailbox -resultsize unlimited | Select Identity, Alias, DisplayName, DistinguishedName, WindowsEmailAddress

Foreach ($mailbox in $mailboxes)
  {
   
   foreach($user in $csv){
   
   
   $email = $user.emailaddress
   write-host  $email
   $user = get-mailbox $email | select -ExpandProperty samaccountname
   $usr = 'ad\'+$user
   write-host $usr

   
    New-Item -Path '\\sserver04\c$\EXO\batches\access\' -Name $user -ItemType "directory"

    Get-ADPermission $Mailbox.identity | where {($_.ExtendedRights -like *Send-As*) -and ($_.User -like $usr)} | Select Identity, User | export-csv -path "\\sserver04\c$\EXO\batches\access\$user\$user-sendasOtherUser.csv" -Append
   
   
   }


   }

8 Replies

· · ·
tfl
Mace
OP
tfl This person is a Verified Professional
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
PowerShell expert
242 Best Answers
575 Helpful Votes
4 How-tos

What, specifically have you tried. When you say it doesn't work, what errors or other output do you get?

0
· · ·
Slav
Anaheim
OP
Slav

Doesn’t collect any data. Also I have to figure out how to make the script processing faster since there is 4k mailboxes it has to go though.

Is there any other way to get Send As for the batch users against all mailboxes?

0
· · ·
Slav
Anaheim
OP
Slav

Also is there a way to run the following command from a ps session ?

Powershell
get-exchange server se* | get-mailbox 
0
· · ·
Slav
Anaheim
OP
Slav

I got the scoping solved. The script is still not working :/

get-Mailbox -resultsize unlimited -RecipientTypeDetails sharedmailbox | where {($_.servername -like 'ser*')}

0
· · ·
Gary M G
Tabasco
OP
Gary M G This person is a Verified Professional
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
PowerShell expert
22 Best Answers
132 Helpful Votes

Can you filter the mailboxes to create a collection of those having a “Send As” value which is not null?

If so, this might reduce the number of iterations.

0
· · ·
Slav
Anaheim
OP
Slav

An update. I figure out how to loop through users to get their Send-As on shared mailboxes. The script is still very slow and the pssession the script creates dies after two hours.

Anyone knows how to change the timeout for Ps Sessions?

0
· · ·
Mike in IT
Chipotle
OP
Mike in IT
PowerShell expert
57 Best Answers
161 Helpful Votes

You've made updates to the script but if you're still doing this...

Powershell
Foreach ($mailbox in $mailboxes) {
   
    foreach ($user in $csv) {
        #Do stuff....
    }

}

That's likely your problem with efficiency.

If $mailboxes returns 300 mailboxes and $csv has 100 records in it your loop is processing 30,000 times.

For each iteration of $mailbox you're looping through all 100 records in the CSV file again like so;

$mailbox iteration 1/300 ---- user 1-100

$mailbox iteration 2/300 --- user 1-100

...<

$mailbox iteration 50/300 --- user 1-100

$mailbox iteration 51/300 --- user 1-100

etc.etc.

Re-evaluate your loop(s)

0
· · ·
Slav
Anaheim
OP
Slav

I have the script fixed. But it takes forever to complete. 27 users took 24 hours to process. Still better the nothing.

0
Oops, something's wrong below.