daily pastebin goal

Invoke-Command Exchange

Dilydali May 16th, 2018 (edited) 601 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. If ($ForwardEmailTo -ne $NULL) {
  2.     $ExchangeServer = 'ServerName'
  3.     $Block = {
  4.         Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
  5.         Set-Mailbox $args[0] -forwardingaddress $args[1]
  6.     }
  7.     Write-Warning "Forwarding $UserName emails to $ForwardEmailTo"
  8.     Invoke-Command -Session (New-PSSession -ComputerName $ExchangeServer -Authentication Kerberos) -ScriptBlock $Block -ArgumentList $UserName,$ForwardEmailTo
  9.     Get-PSSession | Remove-PSSession
  10.     Write-Verbose "Emails sent to $UserName are now being forwarded to the following user:"
  11. }
  12. Else {
  13.     Write-Warning "Emails for $UserName are not being forwarded. Check who these need to be forwarded to and set this in the Exchange Console."
  14. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand