I'm writing a script that delete redirect mail by shedule. Here http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28316932.html describes how import Exchange function.
Script:
$t = New-JobTrigger –Once –At "08/04/2014 13:58"
$del_redir={
param ([string]$alias)
powershell.exe $ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://myexchsrv.mycompany.local/PowerShell ; Import-PSSession $ExchangeSession ; Set-Mailbox -Identity $alias -ForwardingAddress $null -DeliverToMailboxAndForward $false
}
Register-ScheduledJob -Name Start -ScriptBlock $del_redir -ArgumentList ("usernamealias") -Trigger $t
Separetly lines working.
Set-Mailbox -Identity "usernamealias" -ForwardingAddress $null
work good. Change
Set-Mailbox -Identity $alias -ForwardingAddress $null
at
$t = "d:\scripts\" + $alias + ".txt" #$alias = "usernamealias"
New-Item $t -type file
work too (test input alias)
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://myexchsrv.mycompany.local/PowerShell
Import-PSSession $ExchangeSession
work too (import Exchange function)
Windows PowerShell run as Administrator
All together don't work. Where am I mistaken?