Guest User

Untitled

a guest
Oct 25th, 2021
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. Name......: Autoreply.ps1
  3. Version...: 21.10.1
  4. Author....: Dario CORRADA
  5.  
  6. This script sets an autoreply message in Outlook. In the following example I will set an autoreply from 04:00pm to 09:00am of the day after.
  7. see also https://superuser.com/questions/1683334/scheduled-autoreply/1683591#1683591
  8. #>
  9.  
  10. # elevated script execution with admin privileges
  11. $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
  12. $testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
  13. if ($testadmin -eq $false) {
  14.     Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
  15.     exit $LASTEXITCODE
  16. }
  17.  
  18. # get working directory
  19. $fullname = $MyInvocation.MyCommand.Path
  20. $fullname -match "([a-zA-Z_\-\.\\\s0-9:]+)\\Autoreply\.ps1$" > $null
  21. $workdir = $matches[1]
  22.  
  23. # header
  24. $ErrorActionPreference= 'SilentlyContinue'
  25. Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Bypass -Force
  26. Write-Host "ExecutionPolicy Bypass" -fore Green
  27. $ErrorActionPreference= 'Inquire'
  28. $WarningPreference = 'SilentlyContinue'
  29. Add-Type -AssemblyName System.Windows.Forms
  30. Add-Type -AssemblyName System.Drawing
  31. Add-Type -AssemblyName PresentationFramework
  32. Import-Module -Name "$workdir\Modules\Forms.psm1"
  33.  
  34. # import the EXO V2 module
  35. $ErrorActionPreference= 'Stop'
  36. try {
  37.     Import-Module ExchangeOnlineManagement
  38. } catch {
  39.     Install-Module ExchangeOnlineManagement -Confirm:$False -Force
  40.     Import-Module ExchangeOnlineManagement
  41. }
  42. $ErrorActionPreference= 'Inquire'
  43.  
  44. $answ = [System.Windows.MessageBox]::Show("Close Outlook client and then click Ok...",'WARNING','Ok','Warning')
  45.  
  46. # get credentials
  47. $form = FormBase -w 520 -h 270 -text "ACCOUNT"
  48. $font = New-Object System.Drawing.Font("Arial", 12)
  49. $form.Font = $font
  50. $label = New-Object System.Windows.Forms.Label
  51. $label.Location = New-Object System.Drawing.Point(10,20)
  52. $label.Size = New-Object System.Drawing.Size(500,30)
  53. $label.Text = "Username:"
  54. $form.Controls.Add($label)
  55. $usrname = New-Object System.Windows.Forms.TextBox
  56. $usrname.Location = New-Object System.Drawing.Point(10,60)
  57. $usrname.Size = New-Object System.Drawing.Size(450,30)
  58. $form.Controls.Add($usrname)
  59. $label2 = New-Object System.Windows.Forms.Label
  60. $label2.Location = New-Object System.Drawing.Point(10,100)
  61. $label2.Size = New-Object System.Drawing.Size(500,30)
  62. $label2.Text = "Password:"
  63. $form.Controls.Add($label2)
  64. $passwd = New-Object System.Windows.Forms.MaskedTextBox
  65. $passwd.PasswordChar = '*'
  66. $passwd.Location = New-Object System.Drawing.Point(10,140)
  67. $passwd.Size = New-Object System.Drawing.Size(450,30)
  68. $form.Controls.Add($passwd)
  69. $OKButton = New-Object System.Windows.Forms.Button
  70. OKButton -form $form -x 200 -y 190 -text "Ok"
  71. $form.Topmost = $true
  72. $result = $form.ShowDialog()
  73.  
  74. # setting autoreply
  75. $username = $usrname.Text
  76. $username -match "^([a-zA-Z_\-\.\\\s0-9:]+)@.+$" > $null
  77. $unique = $matches[1]
  78. $password = ConvertTo-SecureString $passwd.Text -AsPlainText -Force
  79. $UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
  80. Connect-ExchangeOnline -Credential $UserCredential
  81. $message = @'
  82. <html> <body> <div  style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rg b(0,0,0)">
  83. <p>Hi there,</p>
  84. <p>currently I am out of office.</p>
  85. <p>I will be available from monday to friday, 09:00-16:00. Preferably, I will reply to your email in such period.</p>
  86. <p>Kind regards</p>
  87. </div> </body> </html>
  88. '@
  89. Set-MailboxAutoReplyConfiguration -Identity $unique -AutoReplyState "Scheduled" -ExternalMessage $message -InternalMessage $message -StartTime (Get-Date -Hour 16 -Minute 0 -Second 0) -EndTime (((Get-Date -Hour 9 -Minute 0 -Second 0).AddDays(1))) -ExternalAudience All
Advertisement
Add Comment
Please, Sign In to add comment