# scripts failes on scheduled task.

**URL:** https://forums.powershell.org/t/scripts-failes-on-scheduled-task/373
**Category:** PowerShell Help
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/scripts-failes-on-scheduled-task/373 "2011-12-31T18:30:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)
#### Post date: [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/scripts-failes-on-scheduled-task/373/1 "2011-12-31T18:30:00Z")

</div>

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 ucdceiw2.ad.uc.edu  
> Set-AdServerSettings -PreferredGlobalCatalog ucdceiw2.ad.uc.edu  
> #  
> 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("&quot;)[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  
> "Alias"  
> "Administrator"  
> "langforl"  
> "NEWSWABS"  
> "pfeffepe"  
> "uetzgw"  
> "perzigaj"  
> "scarbovl"  
> "ridgwath"  
> 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  
> $error.clear()

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  
> $error.clear()  
> 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.

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 9:02pm UTC](https://forums.powershell.org/t/scripts-failes-on-scheduled-task/373/2 "2024-05-16T21:02:33Z")

</div>


