Skip to content
Tech Shizz Logo

An Engineers Blog

  • TechShizz
  • blog

Office 365 – Out of Office Script

Posted on February 23, 2021 By rich No Comments on Office 365 – Out of Office Script

This script is designed to be used with the script password encryption for Office 365. See here https://www.techshizz.com/post/powershell-script-password-ecryption-for-multi-site-administration for more info. 

$rootpath = (get-item '.' ).parent.FullName
$clientname = Get-Content "$rootpathclient.txt" -Raw
$user = Get-Content "$rootpathUserID.txt"
$PasswordFile = "$rootpathPassword.txt"
$KeyFile = "C:ICUAES.key"
$key = Get-Content $KeyFile
$UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
#Import the session 
Write-Host "Connecting to Exchange Online for $clientname"
Import-PSSession $Session -AllowClobber | Out-Null

function Menu {
[email protected]"
1 Set an Out of Office
2 Disable an Out of Office
3 Get status of Out of Office
Q Quit
Select a task by number or Q to quit
"@
Write-Host "Out of Office Configuration" -ForegroundColor Cyan
$r = Read-Host $menu
Switch ($r) {
"1" {
    #Collect Info
$mailbox = Read-Host "Enter the email address for the account to apply an out of office message"
$alternatecontact = Read-Host "Enter the name of the person to contact in their absence"
$tel = Read-Host "Enter the telephone number for the alternate contact"
$email = Read-Host "Enter an alternate email address the user can be contacted on"
#Set the Our of Office
Write-Host "Setting out of office..."
Set-MailboxAutoReplyConfiguration $mailbox -AutoReplyState enabled -ExternalAudience all -InternalMessage "I am out of the office and unable to reply to your email. In my absence please contact $alternatecontact on $tel." -ExternalMessage "I am out of the office and unable to reply to your email. In my absence please contact $alternatecontact on $tel or email $email"
Write-Host "Out of Office has been set."
}
"2" {
    $mailbox = Read-Host "Enter the email address of the account to disable Out Of Office"
#Disable
Write-Host "Disabling Auto reply for $mailbox"
Set-MailboxAutoReplyConfiguration $mailbox -AutoReplyState disabled
Write-Host "Confirming changes made..."
Get-MailboxAutoReplyConfiguration $mailbox
Write-Host "Ensure the value now reads 'Disabled'"
Menu
}
"3" {
    $mailbox = Read-Host "Enter the email address of the account you want to see the status of Out of Office"
#Disable
Write-Host "Checking Auto reply status for $mailbox"
get-MailboxAutoReplyConfiguration $mailbox
Write-Host "You can now review the current out of office status for $mailbox above."
Menu
}
"Q" {
    Write-Host "Quitting" -ForegroundColor Green
    Get-PSSession | Remove-PSSession
    Exit
}
default {
    Write-Host "Choose a valid option... Fool!" -ForegroundColor Yellow
    Menu
}
} #end switch
}
Menu
## The following four lines only need to be declared once in your script.
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Description."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Description."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
 
## Prompt to finsh
$title = "Are you done?"
$message = "Do you want to enable/disable another?"
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result) {
    0{
        Write-Host "Script has completed."
        Get-PSSession | Remove-PSSession
    }1{
        Menu
        }
}
Share

Post navigation

❮ Previous Post: Using PowerShell to Add Drivers / Packages / Install Roles and Features in a .wim file
Next Post: Windows Store is missing in Windows 10 ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Subscribe to our newsletter!

Recent Posts

  • How to implement a lightning-fast ransomware playbook
  • How to achieve defence in depth in your business
  • How to implement a SecOps team phishing response plan
  • How to block an Office 365 Sign-in correctly
  • Microsoft finally patched serious Exchange 0-day over a month old!

Recent Comments

    Archives

    • November 2022
    • July 2021
    • March 2021
    • February 2021

    Categories

    • Cyber Security
    • Uncategorized

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org

    Copyright © 2023 .

    Theme: Oceanly News Dark by ScriptsTown