MSOnline PowerShell Help

Dan Smith 1 Reputation point
2022-08-12T07:39:23.653+00:00

Hey
I'm having issues with my powershell script, it's something that did work in previous company and it's not working now.
I think it's to do with the authentication method, but I can't seem to get it to work

Can someone please assist?

Import-Module MSOnline
$account = $true
while ($account -eq $true)
{
try {
$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange ConnectTo-ExchangeOnline -Office365AdminUsername $Office365Username -Office365AdminPassword $Office365Password
$account = $false
}
catch {
clear-host
write-host "" -ForegroundColor red
write-host "Incorrect Credentials Entered" -ForegroundColor red
write-host "" -ForegroundColor red
read-host "Press Enter to Exit"
exit

}
}

Import-PSSession $Session

What to do when error detected
Function ErrorMessage
{
clear-host
write-host "" -ForegroundColor red
write-host "Error Detected" -ForegroundColor red
write-host "" -ForegroundColor red
}

Sub Menu for adding access to shared Mailboxes
Function SubMenuAddEmailRights
{

Sub Menu System#

$Loop2 = $true
While ($Loop2)
{
clear-host
write-host
write-host ""
write-host " E-Mail Management System"
write-host ""
write-host
write-host
write-host "------------------------"
write-host "Add Email Address Rights"
write-host "------------------------"
write-host
write-host "1) Add Full Access to Shared Mailbox (Automapping)"
Write-host
write-host "2) Add Full Access to Shared Mailbox (Without Automapping)"
write-host
write-host "3) Add SendAs Access to Shared Mailbox"
write-host
write-host "4) Add Full Access AND SendAs Access to shared Mailbox (Automapping)"
write-host
write-host "5) Add Full Access AND SendAs Access to shared Mailbox (Without Automapping)"
write-host
write-host "X) Main Menu"
write-host
write-host

$opt2 = Read-Host "Select an option "
write-host $opt2
switch ($opt2)
{

default
{
clear-host
Write-Host "*************" -ForegroundColor Yellow
Write-Host "Invalid Entry" -ForegroundColor Yellow
Write-Host "*************" -ForegroundColor Yellow
start-sleep 1
}
1
{
$error.clear() #before the command
clear-host
$sharedbox = Read-Host "Enter the shared mailbox address"
$sharedaccess = Read-Host "Enter the address of person requiring access"
Add-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -InheritanceType All -Automapping $True -ErrorAction Stop
write-host "Changes have now been made. "$sharedaccess" now has access to "$sharedbox
if ($error.Count -gt 0){
ErrorMessage
}
read-host "Press Enter to Continue"
}

2
{
$error.clear() #before the command
clear-host
$sharedbox = Read-Host "Enter the shared mailbox address"
$sharedaccess = Read-Host "Enter the address of person requiring access"
Add-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -InheritanceType All -Automapping $False -ErrorAction Stop
write-host "Changes have now been made. "$sharedaccess" now has access to "$sharedbox
if ($error.Count -gt 0){
ErrorMessage
}
read-host "Press Enter to Continue"
}

3
{
$error.clear() #before the command
clear-host
$sharedbox = Read-Host "Enter the shared mailbox address"
$sharedaccess = Read-Host "Enter the address of person requiring SendAs access"
Add-RecipientPermission $sharedbox -AccessRights SendAs -Trustee $sharedaccess -ErrorAction Stop
write-host "Changes have now been made. "$sharedaccess" now has access to send from "$sharedbox
if ($error.Count -gt 0){
ErrorMessage
}
read-host "Press Enter to Continue"
}

4
{
$error.clear() #before the command
clear-host
$sharedbox = Read-Host "Enter the shared mailbox address"
$sharedaccess = Read-Host "Enter the address of person requiring Full Access and SendAs access"
Add-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -InheritanceType All -Automapping $True -ErrorAction Stop
Add-RecipientPermission $sharedbox -AccessRights SendAs -Trustee $sharedaccess -ErrorAction Stop
write-host "Changes have now been made. "$sharedaccess" now has full access to, and is able send from "$sharedbox
if ($error.Count -gt 0){
ErrorMessage
}
read-host "Press Enter to Continue"
}

5
{
$error.clear() #before the command
clear-host
$sharedbox = Read-Host "Enter the shared mailbox address"
$sharedaccess = Read-Host "Enter the address of person requiring Full Access and SendAs access"
Add-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -InheritanceType All -Automapping $False -ErrorAction Stop
Add-RecipientPermission $sharedbox -AccessRights SendAs -Trustee $sharedaccess -ErrorAction Stop
write-host "Changes have now been made. "$sharedaccess" now has full access to, and is able send from "$sharedbox
if ($error.Count -gt 0){
ErrorMessage
}
read-host "Press Enter to Continue"
}

x
{
$Loop2 = $false
}
}
}
}

Sub Menu for removing access to shared Mailboxes
Function SubMenuRemoveEmailRights
{

Sub Menu System#

$Loop2 = $true
While ($Loop2)
{
clear-host
write-host
write-host ""
write-host " E-Mail Management System"
write-host ""
write-host
write-host
write-host "---------------------------"
write-host "Remove Email Address Rights"
write-host "---------------------------"
write-host
write-host "1) Remove Full Access to Shared Mailbox"
write-host
write-host "2) Remove SendAs Access to Shared Mailbox"
write-host
write-host "3) Remove Full Access AND SendAs Access to shared Mailbox"
write-host
write-host "X) Main Menu"
write-host
write-host

$opt2 = Read-Host "Select an option "
write-host $opt2
switch ($opt2)
{

default
{
clear-host
Write-Host "*************" -ForegroundColor Yellow
Write-Host "Invalid Entry" -ForegroundColor Yellow
Write-Host "*************" -ForegroundColor Yellow
start-sleep 1
}
1
{
$error.clear() #before the command
clear-host
$sharedbox = Read-Host "Enter the shared mailbox address"
$sharedaccess = Read-Host "Enter the address of person to remove access"
Remove-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -ErrorAction Stop
write-host "Changes have now been made. "$sharedaccess" no longer has access to "$sharedbox
if ($error.Count -gt 0){
ErrorMessage
}
read-host "Press Enter to Continue"
}

2
{
$error.clear() #before the command
clear-host
$sharedbox = Read-Host "Enter the shared mailbox address"
$sharedaccess = Read-Host "Enter the address of person to remove SendAs access"
Remove-RecipientPermission $sharedbox -AccessRights SendAs -Trustee $sharedaccess -ErrorAction Stop
write-host "Changes have now been made. "$sharedaccess" no longer has access to send from "$sharedbox
if ($error.Count -gt 0){
ErrorMessage
}
read-host "Press Enter to Continue"
}

3
{
$error.clear() #before the command
clear-host
$sharedbox = Read-Host "Enter the shared mailbox address"
$sharedaccess = Read-Host "Enter the address of person to remove Full Access and SendAs access"
Remove-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -ErrorAction Stop
Remove-RecipientPermission $sharedbox -AccessRights SendAs -Trustee $sharedaccess -ErrorAction Stop
write-host "Changes have now been made. "$sharedaccess" no longer has full access to, and no access to send from "$sharedbox
if ($error.Count -gt 0){
ErrorMessage
}
read-host "Press Enter to Continue"
}

x
{
$Loop2 = $false
}
}
}
}

Sub Menu for admin and licencing
Function SubMenuAdmin
{

Sub Menu System#

$Loop2 = $true
While ($Loop2)
{
clear-host
write-host
write-host ""
write-host " E-Mail Management System"
write-host ""
write-host
write-host
write-host "------------------------"
write-host "Admin and Licencing Menu"
write-host "------------------------"
write-host
write-host "1) Display Licence Information"
write-host
write-host "2) Display Any Shared Mailboxes that are licenced"
write-host
write-host "3) Remove All Licences from Shared Mailboxes"
write-host
write-host "X) Main Menu"
write-host
write-host

$opt2 = Read-Host "Select an option "
write-host $opt2
switch ($opt2)
{

default
{
clear-host
Write-Host "*************" -ForegroundColor Yellow
Write-Host "Invalid Entry" -ForegroundColor Yellow
Write-Host "*************" -ForegroundColor Yellow
start-sleep 1
}
1
{
Get-MsolAccountSku
write-host
read-Host "Press Enter to Continue"
}
2
{
clear-host
write-host "Please Wait. Processing"
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $.isLicensed -eq "TRUE" }
read-Host "Press Enter to Continue"
}
3
{
clear-host
write-host "Please Wait. This Process Takes a Long Time.(Go make a cup of tea)"
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $
.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $.UserPrincipalName -RemoveLicenses "ENTERPRISEPACK"}
clear-host
write-host "Please Wait. This Process Takes a Long Time.(I'm still working)"
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $
.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $.UserPrincipalName -RemoveLicenses "VISIOCLIENT"}
clear-host
write-host "Please Wait. This Process Takes a Long Time.(Enjoy that cup of tea)"
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $
.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $.UserPrincipalName -RemoveLicenses "EMSPREMIUM"}
clear-host
write-host "Please Wait. This Process Takes a Long Time.(Still going.....)"
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $
.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $.UserPrincipalName -RemoveLicenses "PROJECTCLIENT"}
clear-host
write-host "Please Wait. This Process Takes a Long Time.(*YAWN* Still going)"
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $
.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $.UserPrincipalName -RemoveLicenses "ENTERPRISEPREMIUM"}
clear-host
write-host "Please Wait. This Process Takes a Long Time.(Half way there now)"
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $
.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "POWER_BI_PRO"}
clear-host
write-host "Please Wait. This Process Takes a Long Time.(Go make another tea)"
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $.UserPrincipalName -RemoveLicenses "POWER_BI_STANDARD"}
clear-host
write-host "Please Wait. This Process Takes a Long Time.(Can't be too long now)"
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $.UserPrincipalName -RemoveLicenses ":EMS"}
clear-host
write-host "Please Wait. This Process Takes a Long Time.(Almost done)"
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $.UserPrincipalName -RemoveLicenses "PROJECTPROFESSIONAL"}
clear-host
write-host "All Licences Removed from Shared Mailboxes"
read-Host "Press Enter to Continue"
}
x
{
$Loop2 = $false
}
}
}
}

Main Menu System

$Loop1 = $true
While ($Loop1)
{
clear-host
write-host
write-host ""
write-host " E-Mail Management System"
write-host ""
write-host
write-host
write-host "---------"
write-host "Main Menu"
write-host "---------"
write-host
write-host "1) Add Mailbox Permissions"
write-host
write-host "2) Remove Mailbox Permissions"
write-host
write-host "A) Admin and Reporting/Licencing"
write-host
write-host "X) Exit and Disconnect Sessions"
write-host
write-host

$opt1 = Read-Host "Select an option "
write-host $opt1
switch ($opt1)
{

default
{
clear-host
Write-Host "*************" -ForegroundColor Yellow
Write-Host "Invalid Entry" -ForegroundColor Yellow
Write-Host "*************" -ForegroundColor Yellow
start-sleep 1
}
1
{
SubMenuAddEmailRights
}
2
{
SubMenuRemoveEmailRights
}
a
{
SubMenuAdmin
}

x
{
Function Disconnect-ExchangeOnline {Get-PSSession | Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"} | Remove-PSSession}
Disconnect-ExchangeOnline -confirm
clear-host
write-host "Thank you for using"
$Loop1 = $true
Exit
}
}
}

Exchange | Exchange Server | Management
Windows for business | Windows Server | User experience | PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. Dan Smith 1 Reputation point
    2022-08-12T11:39:02.57+00:00

    Apologies here is the script

    The error i'm getting is regardless of what account it says the credentials are incorrect even though they are.

    Import-Module MSOnline
    $account = $true
    while ($account -eq $true)
    {
    try {
    $UserCredential = Get-Credential
    Connect-MsolService -Credential $UserCredential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange ConnectTo-ExchangeOnline -Office365AdminUsername $Office365Username -Office365AdminPassword $Office365Password
    $account = $false
    }
    catch {
    clear-host
    write-host "*****************************" -ForegroundColor red
    write-host "Incorrect Credentials Entered" -ForegroundColor red
    write-host "*****************************" -ForegroundColor red
    read-host "Press Enter to Exit"
    exit

    }  
    

    }

    Import-PSSession $Session

    What to do when error detected

    Function ErrorMessage
    {
    clear-host
    write-host "**************" -ForegroundColor red
    write-host "Error Detected" -ForegroundColor red
    write-host "**************" -ForegroundColor red
    }

    Sub Menu for adding access to shared Mailboxes

    Function SubMenuAddEmailRights
    {
    #Sub Menu System#
    $Loop2 = $true
    While ($Loop2)
    {
    clear-host
    write-host
    write-host "******************************************************************"
    write-host " E-Mail Management System"
    write-host "******************************************************************"
    write-host
    write-host
    write-host "------------------------"
    write-host "Add Email Address Rights"
    write-host "------------------------"
    write-host
    write-host "1) Add Full Access to Shared Mailbox (Automapping)"
    Write-host
    write-host "2) Add Full Access to Shared Mailbox (Without Automapping)"
    write-host
    write-host "3) Add SendAs Access to Shared Mailbox"
    write-host
    write-host "4) Add Full Access AND SendAs Access to shared Mailbox (Automapping)"
    write-host
    write-host "5) Add Full Access AND SendAs Access to shared Mailbox (Without Automapping)"
    write-host
    write-host "X) Main Menu"
    write-host
    write-host

    $opt2 = Read-Host "Select an option "  
    write-host $opt2  
    switch ($opt2)  
    

    {

    default   
    {  
    clear-host  
    Write-Host "*************" -ForegroundColor Yellow  
    Write-Host "Invalid Entry" -ForegroundColor Yellow  
    Write-Host "*************" -ForegroundColor Yellow  
    start-sleep 1  
    }  
    
    1  
    {  
        $error.clear() #before the command  
        clear-host  
        $sharedbox = Read-Host "Enter the shared mailbox address"  
        $sharedaccess = Read-Host "Enter the address of person requiring access"  
        Add-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -InheritanceType All -Automapping $True -ErrorAction Stop   
        write-host "Changes have now been made. "$sharedaccess" now has access to "$sharedbox  
    

    if ($error.Count -gt 0){
    ErrorMessage
    }
    read-host "Press Enter to Continue"
    }

    2  
    {  
        $error.clear() #before the command  
        clear-host  
        $sharedbox = Read-Host "Enter the shared mailbox address"  
        $sharedaccess = Read-Host "Enter the address of person requiring access"  
        Add-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -InheritanceType All -Automapping $False -ErrorAction Stop   
        write-host "Changes have now been made. "$sharedaccess" now has access to "$sharedbox  
    

    if ($error.Count -gt 0){
    ErrorMessage
    }
    read-host "Press Enter to Continue"
    }

    3  
    {  
    $error.clear() #before the command  
    clear-host  
    $sharedbox = Read-Host "Enter the shared mailbox address"  
    $sharedaccess = Read-Host "Enter the address of person requiring SendAs access"  
    Add-RecipientPermission $sharedbox -AccessRights SendAs -Trustee $sharedaccess -ErrorAction Stop  
    write-host "Changes have now been made. "$sharedaccess" now has access to send from "$sharedbox  
    

    if ($error.Count -gt 0){
    ErrorMessage
    }
    read-host "Press Enter to Continue"
    }

    4  
    {  
    $error.clear() #before the command  
    clear-host  
    $sharedbox = Read-Host "Enter the shared mailbox address"  
    $sharedaccess = Read-Host "Enter the address of person requiring Full Access and SendAs access"  
    Add-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -InheritanceType All -Automapping $True -ErrorAction Stop   
    Add-RecipientPermission $sharedbox -AccessRights SendAs -Trustee $sharedaccess -ErrorAction Stop  
    write-host "Changes have now been made. "$sharedaccess" now has full access to, and is able send from "$sharedbox  
    

    if ($error.Count -gt 0){
    ErrorMessage
    }
    read-host "Press Enter to Continue"
    }

    5  
    {  
    $error.clear() #before the command  
    clear-host  
    $sharedbox = Read-Host "Enter the shared mailbox address"  
    $sharedaccess = Read-Host "Enter the address of person requiring Full Access and SendAs access"  
    Add-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -InheritanceType All -Automapping $False -ErrorAction Stop   
    Add-RecipientPermission $sharedbox -AccessRights SendAs -Trustee $sharedaccess -ErrorAction Stop  
    write-host "Changes have now been made. "$sharedaccess" now has full access to, and is able send from "$sharedbox  
    

    if ($error.Count -gt 0){
    ErrorMessage
    }
    read-host "Press Enter to Continue"
    }

    x  
    {  
    $Loop2 = $false  
    }  
    
    }  
    }  
    

    }

    Sub Menu for removing access to shared Mailboxes

    Function SubMenuRemoveEmailRights
    {
    #Sub Menu System#
    $Loop2 = $true
    While ($Loop2)
    {
    clear-host
    write-host
    write-host "******************************************************************"
    write-host " E-Mail Management System"
    write-host "******************************************************************"
    write-host
    write-host
    write-host "---------------------------"
    write-host "Remove Email Address Rights"
    write-host "---------------------------"
    write-host
    write-host "1) Remove Full Access to Shared Mailbox"
    write-host
    write-host "2) Remove SendAs Access to Shared Mailbox"
    write-host
    write-host "3) Remove Full Access AND SendAs Access to shared Mailbox"
    write-host
    write-host "X) Main Menu"
    write-host
    write-host

    $opt2 = Read-Host "Select an option "  
    write-host $opt2  
    switch ($opt2)  
    

    {

    default   
    {  
    clear-host  
    Write-Host "*************" -ForegroundColor Yellow  
    Write-Host "Invalid Entry" -ForegroundColor Yellow  
    Write-Host "*************" -ForegroundColor Yellow  
    start-sleep 1  
    }  
    
    1  
    {  
        $error.clear() #before the command  
        clear-host  
        $sharedbox = Read-Host "Enter the shared mailbox address"  
        $sharedaccess = Read-Host "Enter the address of person to remove access"  
        Remove-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -ErrorAction Stop   
        write-host "Changes have now been made. "$sharedaccess" no longer has access to "$sharedbox  
    

    if ($error.Count -gt 0){
    ErrorMessage
    }
    read-host "Press Enter to Continue"
    }

    2  
    {  
    $error.clear() #before the command  
    clear-host  
    $sharedbox = Read-Host "Enter the shared mailbox address"  
    $sharedaccess = Read-Host "Enter the address of person to remove SendAs access"  
    Remove-RecipientPermission $sharedbox -AccessRights SendAs -Trustee $sharedaccess -ErrorAction Stop  
    write-host "Changes have now been made. "$sharedaccess" no longer has access to send from "$sharedbox  
    

    if ($error.Count -gt 0){
    ErrorMessage
    }
    read-host "Press Enter to Continue"
    }

    3  
    {  
    $error.clear() #before the command  
    clear-host  
    $sharedbox = Read-Host "Enter the shared mailbox address"  
    $sharedaccess = Read-Host "Enter the address of person to remove Full Access and SendAs access"  
    Remove-MailboxPermission -Identity $sharedbox -User $sharedaccess -AccessRights FullAccess -ErrorAction Stop   
    Remove-RecipientPermission $sharedbox -AccessRights SendAs -Trustee $sharedaccess -ErrorAction Stop  
    write-host "Changes have now been made. "$sharedaccess" no longer has full access to, and no access to send from "$sharedbox  
    

    if ($error.Count -gt 0){
    ErrorMessage
    }
    read-host "Press Enter to Continue"
    }

    x  
    {  
    $Loop2 = $false  
    }  
    
    }  
    }  
    

    }

    Sub Menu for admin and licencing

    Function SubMenuAdmin
    {
    #Sub Menu System#
    $Loop2 = $true
    While ($Loop2)
    {
    clear-host
    write-host
    write-host "******************************************************************"
    write-host " E-Mail Management System"
    write-host "******************************************************************"
    write-host
    write-host
    write-host "------------------------"
    write-host "Admin and Licencing Menu"
    write-host "------------------------"
    write-host
    write-host "1) Display Licence Information"
    write-host
    write-host "2) Display Any Shared Mailboxes that are licenced"
    write-host
    write-host "3) Remove All Licences from Shared Mailboxes"
    write-host
    write-host "X) Main Menu"
    write-host
    write-host

    $opt2 = Read-Host "Select an option "  
    write-host $opt2  
    switch ($opt2)  
    

    {

    default   
    {  
    clear-host  
    Write-Host "*************" -ForegroundColor Yellow  
    Write-Host "Invalid Entry" -ForegroundColor Yellow  
    Write-Host "*************" -ForegroundColor Yellow  
    start-sleep 1  
    }  
    
    1  
    {  
    Get-MsolAccountSku  
    write-host  
    read-Host "Press Enter to Continue"  
    }  
    
    2  
    {  
    clear-host  
    write-host "Please Wait. Processing"  
    Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" }  
    read-Host "Press Enter to Continue"  
    }  
    
    3  
    {  
    clear-host  
    write-host "Please Wait. This Process Takes a Long Time.(Go make a cup of tea)"  
    Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "ENTERPRISEPACK"}  
    clear-host  
    write-host "Please Wait. This Process Takes a Long Time.(I'm still working)"  
    Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "VISIOCLIENT"}  
    clear-host  
    write-host "Please Wait. This Process Takes a Long Time.(Enjoy that cup of tea)"  
    Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "EMSPREMIUM"}  
    clear-host  
    write-host "Please Wait. This Process Takes a Long Time.(Still going.....)"  
    Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "PROJECTCLIENT"}  
    clear-host  
    write-host "Please Wait. This Process Takes a Long Time.(*YAWN* Still going)"  
    Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "ENTERPRISEPREMIUM"}  
    clear-host  
    write-host "Please Wait. This Process Takes a Long Time.(Half way there now)"  
    Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "POWER_BI_PRO"}  
    clear-host  
    write-host "Please Wait. This Process Takes a Long Time.(Go make another tea)"  
    Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "POWER_BI_STANDARD"}  
    clear-host  
    write-host "Please Wait. This Process Takes a Long Time.(Can't be too long now)"  
    Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses ":EMS"}  
    clear-host  
    write-host "Please Wait. This Process Takes a Long Time.(Almost done)"  
    Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" } | foreach {Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "PROJECTPROFESSIONAL"}  
    clear-host  
    write-host "All Licences Removed from Shared Mailboxes"  
    read-Host "Press Enter to Continue"  
    }  
    
    x  
    {  
    $Loop2 = $false  
    }  
    
    }  
    }  
    

    }

    Main Menu System#

    $Loop1 = $true
    While ($Loop1)
    {
    clear-host
    write-host
    write-host "******************************************************************"
    write-host " E-Mail Management System"
    write-host "******************************************************************"
    write-host
    write-host
    write-host "---------"
    write-host "Main Menu"
    write-host "---------"
    write-host
    write-host "1) Add Mailbox Permissions"
    write-host
    write-host "2) Remove Mailbox Permissions"
    write-host
    write-host "A) Admin and Reporting/Licencing"
    write-host
    write-host "X) Exit and Disconnect Sessions"
    write-host
    write-host

    $opt1 = Read-Host "Select an option "  
    write-host $opt1  
    switch ($opt1)  
    

    {

    default   
    {  
    clear-host  
    Write-Host "*************" -ForegroundColor Yellow  
    Write-Host "Invalid Entry" -ForegroundColor Yellow  
    Write-Host "*************" -ForegroundColor Yellow  
    start-sleep 1  
    }  
    
    1  
    {  
    SubMenuAddEmailRights  
    }  
    
    2  
    {  
    SubMenuRemoveEmailRights  
    }  
    
    a  
    {  
    SubMenuAdmin  
    }  
    
    x  
    {  
    Function Disconnect-ExchangeOnline {Get-PSSession | Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"} | Remove-PSSession}  
    Disconnect-ExchangeOnline -confirm  
    clear-host  
    write-host "Thank you for using"  
    $Loop1 = $true  
    Exit  
    }  
    

    }
    }

    0 comments No comments

  2. Andy David - MVP 157.8K Reputation points MVP Volunteer Moderator
    2022-08-12T11:48:32.667+00:00

    If you are connecting to Exchange Online, I dont see where you set the credentials.
    Why is this connecting to MSolService?

    $UserCredential = Get-Credential
    Connect-MsolService -Credential $UserCredential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange ConnectTo-ExchangeOnline -Office365AdminUsername $Office365Username -Office365AdminPassword $Office365Password
    $account = $false

    The better way for this is to use certificate based auth and the V2 Exchange Online module:

    https://learn.microsoft.com/en-us/powershell/exchange/app-only-auth-powershell-v2?view=exchange-ps

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.