Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param(
- [string]$username,
- [string]$LastDay
- )
- $Email = Read-Host "Is there a Forwarding Address? "
- # Password
- . "F:\powershell\encPassword\Password.ps1"
- $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($MyCredential.Password)
- $uenc = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
- ## Get the Mailbox to Set Auto-Reply on
- $MailboxName = $username
- #$MailboxName = Read-Host "Who are we Setting Auto-Reply on"
- #Set other Variables
- $Account = Get-ADUser -Identity $MailboxName
- $FName = $Account.GivenName
- $LName = $Account.Surname
- $FullName = "$FName $LName"
- #$DomainLogon = "domain\user"
- $DomainLogon = "domain\$($MyCredential.UserName)"
- $RuleName = "$MailboxName Auto-Reply Rule"
- try {
- Get-PSSession | Remove-PSSession
- }
- catch {
- "No Existing Remote Exchange Sessisons to Terminate"
- }
- $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<fq exchange server>/PowerShell/ -Authentication Kerberos -Credential $myCredential
- Import-PSSession $Session -AllowClobber
- #Get Current User credentials and setup exchange session
- $Mailuri = Get-Mailbox -Identity $MailboxName
- try {
- Get-PSSession | Remove-PSSession
- }
- catch {
- "No Existing Remote Exchange Sessisons to Terminate"
- }
- #$userCredential = Get-Credential -credential "$DomainLogon"
- $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://$($Mailuri.ServerName).<fq exchange server domain minus server name>/PowerShell/" -Authentication Kerberos -Credential $myCredential
- Import-PSSession $Session -AllowClobber
- #Grant Impersonation rights in Exchange Management Shell
- Add-ADPermission -Identity $MailboxName -User $DomainLogon -extendedRight ms-Exch-EPI-May-Impersonate
- #Give Yourself Full Mailbox rights on User
- Add-MailboxPermission -Identity $MailboxName -User '<username (you)>' -AccessRights fullaccess
- ## Load Managed API dll
- Add-Type -Path 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'
- ## Set Exchange Version
- $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2 #Exchange2016 #Exchange2013_SP2
- ## Create Exchange Service Object
- $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
- ## Set Credentials to use two options are available Options 1 to use explicit credentials or Option 2 use the Default (logged On) credentials
- #Credentials Option 1 using UPN for the windows Account
- #$psCred = Get-Credential
- $psCred = $myCredential
- $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
- $service.Credentials = $creds
- #Credentials Option 2
- #service.UseDefaultCredentials = $true
- ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates
- ## Code From http://poshcode.org/624
- ## Create a compilation environment
- $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
- $Compiler=$Provider.CreateCompiler()
- $Params=New-Object System.CodeDom.Compiler.CompilerParameters
- $Params.GenerateExecutable=$False
- $Params.GenerateInMemory=$True
- $Params.IncludeDebugInformation=$true #$False
- $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
- $TASource=@'
- namespace Local.ToolkitExtensions.Net.CertificatePolicy{
- public class TrustAll : System.Net.ICertificatePolicy {
- public TrustAll() {
- }
- public bool CheckValidationResult(System.Net.ServicePoint sp,
- System.Security.Cryptography.X509Certificates.X509Certificate cert,
- System.Net.WebRequest req, int problem) {
- return true;
- }
- }
- }
- '@
- $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
- $TAAssembly=$TAResults.CompiledAssembly
- ## We now create an instance of the TrustAll and attach it to the ServicePointManager
- $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
- [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
- ## end code from http://poshcode.org/624
- ## Set the URL of the CAS (Client Access Server) to use two options are availabe to use Autodiscover to find the CAS URL or Hardcode the CAS to use
- #CAS URL Option 1 Autodiscover
- $CASADEmail = "$MailboxName@<email domain>"
- $service.AutodiscoverUrl($CASADEmail,{$true})
- "Using CAS Server : " + $Service.url
- #CAS URL Option 2 Hardcoded
- #$uri=[system.URI] "https://casservername/ews/exchange.asmx"
- #$service.Url = $uri
- ## Optional section for Exchange Impersonation
- $service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $CASADEmail)
- $tmTemplateEmail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service
- $tmTemplateEmail.ItemClass = "IPM.Note.Rules.ReplyTemplate.Microsoft";
- $tmTemplateEmail.IsAssociated = $true;
- $tmTemplateEmail.Subject = "Please Update Contact Information";
- #switch here depending on type of exit, change variable autoreply based on type
- #query AD manager
- #$InCharge
- #Query AD manager phone
- #$ICPhone
- #Query AD manager email
- #$ICEmail
- #if ($type -eq "M") { } else { }
- if ($Email -eq "Y") {
- $MemberEmail = Read-Host "Please type the forwarding Email"
- if ($MemSubType -eq "H") { $title = "Representative " } else { $title = "Senator " }
- $htmlBodyString = "Effective $LastDay $title$FullName is no longer at the Institution. $Title $LName's new email address is: <a href=`"mailto:$MemberEmail`?Subject=$title$FullName`">$MemberEmail</a>. To contact your new district representative, please go to <a href=`"<website>.`"><website>.</a>."
- } else {
- $htmlBodyString = "Effective $LastDay $title$FullName is no longer at the Institution. To contact your new district representative, please go to <a href=`"<website>.`"><website>.</a>."
- }
- #Write-Host $htmlBodyString
- $tmTemplateEmail.Body = New-Object Microsoft.Exchange.WebServices.Data.MessageBody($htmlBodyString);
- $PidTagReplyTemplateId = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x65C2, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary);
- $tmTemplateEmail.SetExtendedProperty($PidTagReplyTemplateId, [System.Guid]::NewGuid().ToByteArray());
- $tmTemplateEmail.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox);
- $nrNewInboxRule = New-Object Microsoft.Exchange.WebServices.Data.Rule;
- $nrNewInboxRule.DisplayName = $RuleName;
- $nrNewInboxRule.Conditions.SentToOrCcMe = $true;
- $nrNewInboxRule.Exceptions.FromAddresses.Add("<ISDgroupemail@domain>");
- $nrNewInboxRule.Actions.ServerReplyWithMessage = $tmTemplateEmail.Id;
- $nrNewInboxRule.Actions.Delete = $true;
- $cnCreateNewRule = New-Object Microsoft.Exchange.WebServices.Data.createRuleOperation[] 1;
- $cnCreateNewRule[0] = $nrNewInboxRule;
- $service.TraceEnabled = $True;
- $service.UpdateInboxRules($cnCreateNewRule,$true);
- #Revoke Full Mailbox rights on User for Yourself
- Remove-MailboxPermission -Identity $MailboxName -User '<username (you)>' -AccessRights fullaccess -Confirm:$False
- #Verify Rule was created
- # Get-InboxRule -Mailbox $MailboxName
- #Remove Rule
- <#
- try
- {
- Remove-InboxRule -Mailbox $MailboxName -Identity $RuleName -Confirm:$False -ErrorAction Stop
- }
- catch
- {
- Write-Host "Rule cannot be found or has already been removed"
- break
- }
- #>
- <#
- ## Most current Exchange API enumeration strings
- <!-- Enumeration of Exchange Server versions -->
- <xs:simpleType name="ExchangeVersionType">
- <xs:restriction base="xs:string">
- <xs:enumeration value="Exchange2007" />
- <xs:enumeration value="Exchange2007_SP1" />
- <xs:enumeration value="Exchange2009" />
- <xs:enumeration value="Exchange2010" />
- <xs:enumeration value="Exchange2010_SP1" />
- <xs:enumeration value="Exchange2010_SP2" />
- <xs:enumeration value="Exchange2012" />
- <xs:enumeration value="Exchange2013" />
- <xs:enumeration value="Exchange2013_SP1" />
- <xs:enumeration value="Exchange2015" />
- <xs:enumeration value="Exchange2016" />
- <xs:enumeration value="V2015_10_05" />
- <xs:enumeration value="V2016_01_06" />
- <xs:enumeration value="V2016_04_13" />
- <xs:enumeration value="V2016_07_13" />
- <xs:enumeration value="V2016_10_10" />
- </xs:restriction>
- </xs:simpleType>
- #>
- #Open Webmail of Exiting User
- <#
- #Give Yourself Full Mailbox rights on User
- $MailboxName = '<person leaving>'
- Add-MailboxPermission -Identity $MailboxName -User '<username (you)>' -AccessRights fullaccess -a
- sleep -s 3
- & C:\Users\<username on pc(you, if you use chrome)>\AppData\Local\Google\Chrome\Application\chrome.exe domainmail <website>/owa/$MailboxName@<domain>/?offline=disabled#path=/options/inboxrules
- #>
- $MailboxName = ''
- Add-MailboxPermission -Identity $MailboxName -User '' -AccessRights fullaccess -AutoMapping $False
- Remove-MailboxPermission -Identity $MailboxName -User '' -AccessRights fullaccess -Confirm:$False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement