Skip to content

Commit 7f4442d

Browse files
authored
Initial functionality (#1)
1 parent ee8df24 commit 7f4442d

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
11
# Office365-send-as
2+
23
A PowerShell script to set Send As permissions for Office365 users and groups
4+
5+
## Usage
6+
7+
### Prerequisites
8+
9+
- The script uses the `ExchangeOnlineManagement` PowerShell module, which requires a minimum PowerShell version of 7.0.3 to be installed. Powershell 7 is installed by default on any Windows 10 or newer PC released after March 2020. The `ExchangeOnlineManagement` module will install automatically as part of the script if not already present.
10+
- The PowerShell execution policy must be set to run unsigned scripts. While running the script may automatically prompt to change the execution policy, it can be changed in PowerShell by running the command `Set-ExecutionPolicy Unrestricted`.
11+
- Execution policy can be reset after running the script using `Set-ExecutionPolicy RemoteSigned`
12+
13+
### Running the Script
14+
15+
- The script can be run by right clicking the `setSendAs.ps1` file and choose “Run with PowerShell,” but I would advise running the script within a PowerShell window to monitor the output.
16+
- Steps to run it in an open PowerShell window:
17+
- Hit the Windows key and the R key together to open a `Run` dialog box. Type “powershell” in the box and hit the Enter key.
18+
- If execution policy hasn't been set to `Unrestricted` yet, type `Set-ExecutionPolicy Unrestricted` and hit the Enter key.
19+
- Drag and drop the script file into the window to paste the path to the script in the shell. Hit the Enter key.
20+
- It will ask for the admin user email to authenticate to Exchange Online, the user email getting SendAs Trustee permissions, whether setting an individual user `U` or all users in a Office365 group `G`, and the user email or Office 365 group to assign the permission to. Fill in the prompts as desired to set the parameters.
21+
- A Microsoft sign-in window will appear to authenticate with Exchange Online once the parameters are provided.
22+
23+
## License
24+
25+
This project is licensed under the terms of the MIT license.

setSendAs.ps1

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<#
2+
Connect to the specified Exchange Online environment and set the SendAs permission for the specified user or Office 365 group. Will install the EXO management module if it does not exist
3+
#>
4+
5+
Function Connect-EXO {
6+
<#
7+
.SYNOPSIS
8+
Connects to EXO when no connection exists. Checks for EXO v3 module
9+
#>
10+
param(
11+
[Parameter(
12+
Mandatory = $true
13+
)]
14+
[string]$adminUPN
15+
)
16+
17+
process {
18+
# Check if EXO is installed and connect if no connection exists
19+
if ($null -eq (Get-Module -ListAvailable -Name ExchangeOnlineManagement))
20+
{
21+
Write-Host "Exchange Online PowerShell v3 module is requied, do you want to install it?" -ForegroundColor Yellow
22+
23+
$install = Read-Host Do you want to install module? [Y] Yes [N] No
24+
if($install -match "[yY]") {
25+
Write-Host "Installing Exchange Online PowerShell v3 module" -ForegroundColor Cyan
26+
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
27+
}else{
28+
Write-Error "Please install EXO v3 module."
29+
}
30+
}
31+
$ModuleInstalled = Get-Module -ListAvailable -Name ExchangeOnlineManagement
32+
if ($null -ne $ModuleInstalled) {
33+
# Check which version of Exchange Online is installed
34+
if ($ModuleInstalled.version -like "3.*" ) {
35+
# Check if there is a active EXO sessions
36+
if ((Get-ConnectionInformation).tokenStatus -ne 'Active') {
37+
Write-Host 'Connecting to Exchange Online' -ForegroundColor Cyan
38+
Connect-ExchangeOnline -UserPrincipalName $adminUPN -ShowBanner:$false
39+
}
40+
}else{
41+
# Check if there is a active EXO sessions
42+
$psSessions = Get-PSSession | Select-Object -Property State, Name
43+
If (((@($psSessions) -like '@{State=Opened; Name=ExchangeOnlineInternalSession*').Count -gt 0) -ne $true) {
44+
Write-Host 'Connecting to Exchange Online' -ForegroundColor Cyan
45+
Connect-ExchangeOnline -UserPrincipalName $adminUPN
46+
}
47+
}
48+
}else{
49+
Write-Host "Please install EXO v3 module." -ForegroundColor Red
50+
exit 1
51+
}
52+
}
53+
}
54+
55+
$AuthUserName = Read-Host "Admin email to sign in as"
56+
$SendAsEmail = Read-Host "User email to be given SendAs Trustee permission"
57+
$UserOrGroup = Read-Host "Set SendAs for Office 365 group or single user? [G] Group [U] User"
58+
if ($UserOrGroup -match "[gG]") {
59+
$Group = Read-Host "Office 365 group to assign SendAs to"
60+
} elseif ($UserOrGroup -match "[uU]") {
61+
$UserEmail = Read-Host "Email of user to add SendAs?"
62+
} else {
63+
Write-Host "Please specify either G or U to assign SendAs permissions." -ForegroundColor Red
64+
exit 1
65+
}
66+
Connect-EXO $AuthUserName
67+
$SendAsUser = Get-Recipient $SendAsEmail
68+
$CurrentSendAsUsers = Get-RecipientPermission -Trustee $SendAsUser.name
69+
if ($UserOrGroup -match "[gG]") {
70+
try {
71+
$Users = Get-UnifiedGroupLinks -id $Group -LinkType members -ErrorAction stop
72+
} catch {
73+
Write-Host "Unable to process group $Group. Please verify the group exists" -ForegroundColor Red
74+
Write-Host 'Disconnecting from Exchange Online' -ForegroundColor Cyan
75+
Disconnect-ExchangeOnline -Confirm:$false
76+
exit 1
77+
}
78+
ForEach($User in $Users) {
79+
# Check to see if current user is the trustee
80+
if ($User.name -ne $SendAsUser.name) {
81+
# Check to see if current user already is assigned to the trustee
82+
if ($null -eq (Compare-Object $CurrentSendAsUsers.identity $User.name -IncludeEqual -ExcludeDifferent).inputObject) {
83+
Write-Host "Adding permission for $SendAsUser to SendAs $User" -ForegroundColor Cyan
84+
Add-RecipientPermission $User.name -AccessRights SendAs -Trustee $SendAsUser -Confirm:$false
85+
}
86+
}
87+
}
88+
} elseif ($UserOrGroup -match "[uU]") {
89+
try {
90+
$User = Get-Recipient $UserEmail -ErrorAction stop
91+
} catch {
92+
Write-Host "Unable to process user $User. Please verify the user exists" -ForegroundColor Red
93+
Write-Host 'Disconnecting from Exchange Online' -ForegroundColor Cyan
94+
Disconnect-ExchangeOnline -Confirm:$false
95+
exit 1
96+
}
97+
if ($User.name -eq $SendAsUser.name) {
98+
Write-Host "Trustee user and target user $User are the same" -ForegroundColor Yellow
99+
Write-Host 'Disconnecting from Exchange Online' -ForegroundColor Cyan
100+
Disconnect-ExchangeOnline -Confirm:$false
101+
exit
102+
}
103+
# Check to see if current user already is assigned to the trustee
104+
if ($null -eq (Compare-Object $CurrentSendAsUsers.identity $User.name -IncludeEqual -ExcludeDifferent).inputObject) {
105+
Add-RecipientPermission $User.name -AccessRights SendAs -Trustee $SendAsUser -Confirm:$false
106+
} else {
107+
Write-Host "$SendAsUser already has SendAs permissions for $User" -ForegroundColor Cyan
108+
}
109+
}
110+
Write-Host 'Disconnecting from Exchange Online' -ForegroundColor Cyan
111+
Disconnect-ExchangeOnline -Confirm:$false

0 commit comments

Comments
 (0)