0

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • $t = New-JobTrigger –Once –At "10/04/2014 9:30" $cred = Get-Credential rosproject\administrator $oo = New-ScheduledJobOption -RunElevated – alex101000 Apr 10 '14 at 06:52

2 Answers2

0

I see one probable and another possible problem.

First:

$t = New-JobTrigger –Once –At "08/04/2014 13:58"

Then later:

$t = "d:\scripts\" + $alias + ".txt" #$alias = "usernamealias"

It appears you've just destroyed your trigger.

The other possible issue is that your arguments to the scriptblock parameter appear to be wrong (impossible to tell without knowing what the scriptblock looks like), and that -argumentlist parameter is not the last parameter in the command.

The Help on Register-ScheduledJob doesn't say so explicitly, but the other cmdlets that take a scripblock argument and let you specify an argument list require that the argument list be the last parameter, and everything that follows is arguments to the scriptblock. This is so the parser won't confuse arguments to the scriptblock with with might be parameters and arguments to the cmdlet. I suspect the same holds true with Regiester-ScheduledJob, and the -Trigger parameter needs to come before the -Scriptblock parameter.

mjolinor
  • 66,130
  • 7
  • 114
  • 135
0

Working script:

$t = New-JobTrigger –Once –At "10/04/2014 9:30"
$cred = Get-Credential mydomain\administrator
$oo = New-ScheduledJobOption -RunElevated
$del_redir={
param ([string]$alias)
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Set-Mailbox -Identity $alias -ForwardingAddress `$null -DeliverToMailboxAndForward 0"
}
Register-ScheduledJob -Name Start -ScriptBlock $del_redir -ArgumentList ("usernamealias") -Trigger $t -Credential $cred -ScheduledJobOption $oo

rem `$null; $oo = New-ScheduledJobOption -RunElevated