I'm running a simple script to archive SMDR logs using Windows Server 2008 R2's Task Scheduler. The last line of the script is as follows --
dir c:\smdr -Filter *.txt | where-object {$_.basename -eq 'SMDR_Log'} | move-item -destination "c:\smdr\ARCHIVED\SMDR_log_$yesterdayString.txt"
The last cmdlet in the pipeline is renaming the file properly (inserting the date into the filename), but it doesn't move it to the ARCHIVED folder. If I run this command outside of Task Scheduler, everything works great. Permissions are fine on the destination folder.
I can think of a ton of ways to work around this, but I'd really like an explanation for why the file is renamed but stays in its current directory.
Exported Scheduled Task XML
<?xml version="1.0" encoding="UTF-16"?>
-<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task" version="1.3">
-<RegistrationInfo>
<Date>2014-07-14T09:34:56.2252628</Date>
<Author>DOMAIN\taskadmin</Author>
<Description>Aggregates the individual daily call logs into one master log (.csv format)</Description>
</RegistrationInfo>
-<Triggers>
-<CalendarTrigger>
<StartBoundary>2014-07-15T00:00:01</StartBoundary>
<Enabled>true</Enabled>
-<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
-<Principals>
-<Principal id="Author">
<UserId>DOMAIN\taskadmin</UserId>
<LogonType>S4U</LogonType>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
-<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
-<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
-<Actions Context="Author">
-<Exec>
<Command>powershell.exe</Command>
<Arguments>-File "C:\scripts\powershell\AvayaCallLogMaintenance\Combine-SMDRLogs.ps1"</Arguments>
</Exec>
</Actions>
</Task>
UPDATE: I upgraded PowerShell from 2.0 to 4.0 and it seems to be working in my tests. I'll let it run overnight and if that ends up resolving it I'll write it up as an answer.
UPDATE 2: PowerShell 4.0 didn't resolve it. I've now broken up the move-item
command into separate rename-item
and then move-item
commands. I'll let it run tonight and see what happens.