scripts failes on scheduled task.

by pfeffepe at 2012-12-03 11:14:45

I’ve been using powershell for Exchange for sometime now. So not exacltly new to this. I have dozens of scripts running via powershell no problem but this latest one is giving me problem. If I run interactivley works fine. Run by task scheduler doesnt work. I have all the necessary permissions, running it with the admin account as well as checked the "Run with highest Priveleges and even put in the set-excultionpolicy unrestriced. I have the Exchange snapin module loaded into the script. What occurs on this last line is that it creates a 0 byte mobiledevices.csv file. I tried doing it in one statement originally but broke it into two trying to troubleshoot this. It’s almost like some buffering issue which I cannot nail down. Any help would be appreciated.

-PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\V14\bin\exshell.psc1" .\Active_Synch_Users.ps1 set-executionpolicy unrestricted > Active_Synch_Users.log

what happens is I do not get an

Add-PSSnapin Microsoft.Exchange.Management.Powershell.E2010
#Set preferred Server settings so it does not get A&F domain controllers
Set-AdserverSettings -PreferredServer
Set-AdServerSettings -PreferredGlobalCatalog
cd "c:\exchscripts\Active_Synch_users"

del MobileDevices.csv
del MobileDevices.txt
del mobiledevicessummary.txt
#del mailboxes.csv

#New-Item c:\exchscripts\Active_Synch_Users\active_synch_Users.txt -type file

$yesterday = Get-Date (Get-Date).AddDays(-1).toString(‘M/d/yyyy’)
$daybefore = Get-Date (Get-Date).AddDays(-2).toString(‘M/d/yyyy’)
$today = Get-Date (Get-Date).toString(‘M/d/yyyy’)
echo $today
echo $yesterday
echo "getting mailboxes"
#Get-Mailbox -ResultSize:Unlimited | select alias | Export-Csv -Path:"C:\Exchscripts\Active_Synch_Users\mailboxes.csv"
echo "parsing mailboxes, getting active synch devices-import to csv"
import-csv mailboxes.csv | foreach-object {Get-ActiveSyncDeviceStatistics -Mailbox:$.alias | Where {$.LastSuccessSync -gt $yesterday} | Sort-Object -Property DeviceType,Identity | Select-Object @{name="EmailAddress";expression={$_.Identity.ToString().Split("")[0]}},DeviceType, DeviceUserAgent, LastSuccessSync} | Export-Csv -Path:"C:\Exchscripts\Active_Synch_Users\MobileDevices.csv"
by Klaas at 2012-12-04 01:17:25
What’s the Last Run Result of the Scheduled Task? Is it (0x0) ?
by pfeffepe at 2012-12-04 04:40:14
Completes with a The operation completed successfully. (0x0) I have even put some pause statements in it thinking it was a timing issue. It does create a 0 byte CSV output file. I even put in echo statements before and after which it hits.
by Klaas at 2012-12-04 07:20:47
So you first create mailboxes.csv which is then imported with the Import-Csv mailboxes.csv cmdlet? But the Get-Mailbox cmdlet is commented out?
Is it the mobiledevices.csv that is empty or the mailboxes.csv?
Maybe you can check if the cd works with ‘echo $pwd’ or use the complete path to mailboxes.csv in the Import-Csv cmdlet?
by pfeffepe at 2012-12-04 08:26:48
mobiledevices.csv is the one that is empty. The mailbox.csv import file contains a valid list of usernames. Path is fine and input file is fine. Here is a log and I put a head statement so the first few records are shown for the mailboxes.csv

Tuesday, December 04, 2012 12:00:00 AM
Monday, December 03, 2012 12:00:00 AM
getting mailboxes

Drive : C
Provider : Microsoft.PowerShell.Core\FileSystem
ProviderPath : C:\exchscripts\Active_Synch_users
Path : C:\exchscripts\Active_Synch_users

finished creating mailbox.csv
#TYPE Selected.Microsoft.Exchange.Data.Directory.Management.Mailbox
parsing mailboxes, getting active synch devices-import to csv
by Jim at 2012-12-06 11:20:50
I’m not saying this will fix your scheduled task issue, but I’m confused as to why you would need to use the "foreach-object" construct in the pipeline, since you are essentially using a one-liner.
by Aleksandar at 2012-12-17 07:02:08
# this will clear the $error variable
by pfeffepe at 2013-03-19 11:12:46
Sorry about letting this sit. We have been migrating to exchange 2010.

Still haven’t made any progress on this. Here is another sample. If you run the enclosed script from the powershell command line you get both files and their contents. You run it as a schedule task you only get the pete2.csv file that is piping the get-mailbox. The other line get-inboxRule returns a zero byte file.

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
get-InboxRule -mailbox argusad | export-csv pete.csv
get-mailbox argusad | export-csv pete2.csv
by pfeffepe at 2013-03-19 11:36:23
Well. I’m running this on our management box which does not have Exchange loaded and it. If I get onto one of our Exchange servers and create the task it runs fine so I’m wondering if it remote powershell issue.