param( [string]$Identity ) try { # Import credentials and establish a PowerShell session to Exchange Server $serverAddress = Get-Content -Path ".\DefaultServer.txt" $currentDomain = ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).Name $credential = Import-Clixml -Path ".\AdminCredential.xml" $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$serverAddress/PowerShell/ -Authentication Kerberos -Credential $credential Import-PSSession $session -DisableNameChecking # Ensure Identity is in the correct format if ($Identity -notlike "*@*") { $Identity = "$Identity@$currentDomain" } # Prompt the user to decide whether to set an EndTime Write-Host "Do you want to set an EndTime for AutoReply? (yes/no)" -ForegroundColor Cyan $setEndTimeResponse = Read-Host "Enter your choice" if ($setEndTimeResponse -eq "yes") { # Prompt user for EndTime input Write-Host "Please enter the EndTime in the format 'dd/MM/yyyy HH:mm:ss' (e.g., 30/12/2099 23:59:59):" -ForegroundColor Cyan $EndTimeInput = Read-Host "EndTime" try { $EndTime = [datetime]::ParseExact($EndTimeInput, "dd/MM/yyyy HH:mm:ss", [System.Globalization.CultureInfo]::InvariantCulture) Write-Host "EndTime accepted: $EndTime" -ForegroundColor Green # Set AutoReplyState to Scheduled with StartTime and EndTime (messages remain unchanged) $StartTime = Get-Date Set-MailboxAutoReplyConfiguration -Identity $Identity -AutoReplyState Scheduled -StartTime $StartTime -EndTime $EndTime Write-Host "AutoReply scheduled for $Identity from $StartTime to $EndTime." -ForegroundColor Green } catch { Write-Host "Invalid EndTime format. Please ensure it is in 'dd/MM/yyyy HH:mm:ss' format." -ForegroundColor Red return } } else { # Set AutoReply to Enabled without scheduling (indefinite, messages remain unchanged) Set-MailboxAutoReplyConfiguration -Identity $Identity -AutoReplyState Enabled Write-Host "AutoReply enabled indefinitely for $Identity." -ForegroundColor Green } # Retrieve and display the updated AutoReply configuration $autoReplyConfig = Get-MailboxAutoReplyConfiguration -Identity $Identity Write-Host "AutoReply configuration for $Identity" -ForegroundColor Cyan Write-Host "State: $($autoReplyConfig.AutoReplyState)" -ForegroundColor Yellow Write-Host "StartTime: $($autoReplyConfig.StartTime)" -ForegroundColor Yellow Write-Host "EndTime: $($autoReplyConfig.EndTime)" -ForegroundColor Yellow Write-Host "Explanation" -ForegroundColor Gray Write-Host "Autoreply State Enabled means its enabled Indefinitely" -ForegroundColor Gray Write-Host "Autoreply State Scheduled means its enabled till the EndTime" -ForegroundColor Gray # Remove the session after completing tasks Remove-PSSession $session } catch { Write-Host "Failed to configure AutoReply for $Identity. Error:" -ForegroundColor Red Write-Host $_ }