Advertisement
Dilydali

Invoke-Command Exchange

May 16th, 2018
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement