Can someone help me with a script I’ve been trying to craft?<\/p>\n
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:<\/p>\n
$credential = New-Object System.Management.Automation.PsCredential(\"username\",$password)\n\nimport-module activedirectory\n\n$exchangeserver = \"Exchangeservername\"\n\n#$E2K16 = new-pssession -configurationname Microsoft.Exchange -connectionuri \"http://$exchangeserver/PowerShell/\" -authentication kerberos -credential $credential\n$E2K16 = new-pssession -configurationname Microsoft.Exchange -connectionuri \"http://$exchangeserver/PowerShell/\" -authentication kerberos \nimport-pssession $E2K16 -allowclobber -disablenamechecking:$True 2>&1 | out-null\nconnect-PSSession $E2K16\nEnter-PSSession $E2K16\n\n###ALWAYS UPDATE THE PATH###################\n$csv = import-csv \"\\\\sserver04\\c$\\EXO\\batches\\Batch000.csv\"\n$Mailboxes = Get-Mailbox -resultsize unlimited | Select Identity, Alias, DisplayName, DistinguishedName, WindowsEmailAddress\n\nForeach ($mailbox in $mailboxes)\n {\n \n foreach($user in $csv){\n \n \n $email = $user.emailaddress\n write-host $email\n $user = get-mailbox $email | select -ExpandProperty samaccountname\n $usr = 'ad\\'+$user\n write-host $usr\n\n \n New-Item -Path '\\\\sserver04\\c$\\EXO\\batches\\access\\' -Name $user -ItemType \"directory\"\n\n 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\n \n \n }\n\n }\n<\/code><\/pre>","upvoteCount":5,"answerCount":9,"datePublished":"2021-11-27T19:18:06.000Z","author":{"@type":"Person","name":"slav","url":"https://community.spiceworks.com/u/slav"},"suggestedAnswer":[{"@type":"Answer","text":"
Advertisement
Can someone help me with a script I’ve been trying to craft?<\/p>\n
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:<\/p>\n
$credential = New-Object System.Management.Automation.PsCredential(\"username\",$password)\n\nimport-module activedirectory\n\n$exchangeserver = \"Exchangeservername\"\n\n#$E2K16 = new-pssession -configurationname Microsoft.Exchange -connectionuri \"http://$exchangeserver/PowerShell/\" -authentication kerberos -credential $credential\n$E2K16 = new-pssession -configurationname Microsoft.Exchange -connectionuri \"http://$exchangeserver/PowerShell/\" -authentication kerberos \nimport-pssession $E2K16 -allowclobber -disablenamechecking:$True 2>&1 | out-null\nconnect-PSSession $E2K16\nEnter-PSSession $E2K16\n\n###ALWAYS UPDATE THE PATH###################\n$csv = import-csv \"\\\\sserver04\\c$\\EXO\\batches\\Batch000.csv\"\n$Mailboxes = Get-Mailbox -resultsize unlimited | Select Identity, Alias, DisplayName, DistinguishedName, WindowsEmailAddress\n\nForeach ($mailbox in $mailboxes)\n {\n \n foreach($user in $csv){\n \n \n $email = $user.emailaddress\n write-host $email\n $user = get-mailbox $email | select -ExpandProperty samaccountname\n $usr = 'ad\\'+$user\n write-host $usr\n\n \n New-Item -Path '\\\\sserver04\\c$\\EXO\\batches\\access\\' -Name $user -ItemType \"directory\"\n\n 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\n \n \n }\n\n }\n<\/code><\/pre>","upvoteCount":5,"datePublished":"2021-11-27T19:18:06.000Z","url":"https://community.spiceworks.com/t/sendas-loop/818102/1","author":{"@type":"Person","name":"slav","url":"https://community.spiceworks.com/u/slav"}},{"@type":"Answer","text":"What, specifically have you tried. When you say it doesn’t work, what errors or other output do you get?<\/p>","upvoteCount":0,"datePublished":"2021-11-27T19:36:27.000Z","url":"https://community.spiceworks.com/t/sendas-loop/818102/2","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":"
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.<\/p>\n
Is there any other way to get Send As for the batch users against all mailboxes?<\/p>","upvoteCount":0,"datePublished":"2021-11-27T20:26:03.000Z","url":"https://community.spiceworks.com/t/sendas-loop/818102/3","author":{"@type":"Person","name":"slav","url":"https://community.spiceworks.com/u/slav"}},{"@type":"Answer","text":"
Also is there a way to run the following command from a ps session ?<\/p>\n
get-exchange server se* | get-mailbox \n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-11-28T14:30:49.000Z","url":"https://community.spiceworks.com/t/sendas-loop/818102/4","author":{"@type":"Person","name":"slav","url":"https://community.spiceworks.com/u/slav"}},{"@type":"Answer","text":"I got the scoping solved. The script is still not working
<\/p>\n
get-Mailbox -resultsize unlimited -RecipientTypeDetails sharedmailbox | where {($_.servername -like ‘ser*’)}<\/p>","upvoteCount":0,"datePublished":"2021-11-28T14:32:42.000Z","url":"https://community.spiceworks.com/t/sendas-loop/818102/5","author":{"@type":"Person","name":"slav","url":"https://community.spiceworks.com/u/slav"}},{"@type":"Answer","text":"
Can you filter the mailboxes to create a collection of those having a “Send As” value which is not null?<\/p>\n
If so, this might reduce the number of iterations.<\/p>","upvoteCount":0,"datePublished":"2021-11-28T18:06:20.000Z","url":"https://community.spiceworks.com/t/sendas-loop/818102/6","author":{"@type":"Person","name":"gary-m-g","url":"https://community.spiceworks.com/u/gary-m-g"}},{"@type":"Answer","text":"
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.<\/p>\n
Anyone knows how to change the timeout for Ps Sessions?<\/p>","upvoteCount":0,"datePublished":"2021-11-29T00:27:55.000Z","url":"https://community.spiceworks.com/t/sendas-loop/818102/7","author":{"@type":"Person","name":"slav","url":"https://community.spiceworks.com/u/slav"}},{"@type":"Answer","text":"
You’ve made updates to the script but if you’re still doing this…<\/p>\n
Foreach ($mailbox in $mailboxes) {\n \n foreach ($user in $csv) {\n #Do stuff....\n }\n\n}\n<\/code><\/pre>\nThat’s likely your problem with efficiency.<\/p>\n
If $mailboxes returns 300 mailboxes and $csv has 100 records in it your loop is processing 30,000 times.<\/p>\n
For each iteration of $mailbox you’re looping through all 100 records in the CSV file again like so;<\/p>\n
$mailbox iteration 1/300 ---- user 1-100<\/p>\n
$mailbox iteration 2/300 — user 1-100<\/p>\n
…<<\/p>\n
$mailbox iteration 50/300 — user 1-100<\/p>\n
$mailbox iteration 51/300 — user 1-100<\/p>\n
etc.etc.<\/p>\n
Re-evaluate your loop(s)<\/p>","upvoteCount":0,"datePublished":"2021-12-04T03:59:42.000Z","url":"https://community.spiceworks.com/t/sendas-loop/818102/8","author":{"@type":"Person","name":"mike9019","url":"https://community.spiceworks.com/u/mike9019"}},{"@type":"Answer","text":"
I have the script fixed. But it takes forever to complete. 27 users took 24 hours to process. Still better the nothing.<\/p>","upvoteCount":0,"datePublished":"2021-12-04T04:02:37.000Z","url":"https://community.spiceworks.com/t/sendas-loop/818102/9","author":{"@type":"Person","name":"slav","url":"https://community.spiceworks.com/u/slav"}}]}}