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:

$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
   
   
   }

   }
5 Spice ups

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

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?

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

get-exchange server se* | get-mailbox 

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

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

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.

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?

You’ve made updates to the script but if you’re still doing this…

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)

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