CANNOT Get PShell Working On Windows 2012 R2 Via Task Scheduler

Am trying to run script to archive files - the job runs perfectly running manually & through command prompt, but will not complete when run via Task Scheduler (even though it says the job ran).

I have seen (many) articles with switches and different ways to get around this but no luck.   Have confirmed account has "log on as batch" rights and also a local admin.   Also has rights to the network folder.

function doTheSort([string]$SourceDir, [string]$DestinationDir) 
{$files = get-childitem $SourceDir -Include @("*.txt","*.log", "*.gtm", "*.x340", "p*") 
 #$files = get-childitem $SourceDir -Exclude @("Archive","BAI Files","ICMS Output","restore","TEST-ARCH")
    foreach ($file in $files)
        $Directory = $DestinationDir + "" + $file.lastwritetime.Date.ToString('yyyy') + "\" + $file.lastwritetime.Date.ToString('MMM') + "\" + $file.lastwritetime.Date.ToString('dd')

        if (!(Test-Path $Directory))
            New-Item $directory -type directory
        Copy-Item $file.fullname $Directory 

doTheSort "I:\Prior Day files for Upload" "C:\DIR-TEST\"

Open in new window

Any suggestions?
Michael O'HaraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Have you tried to run it "only when logged in", and assigning the same account which you use to log in, and then trigger the task manually with -NoExit?

The task status (sadly) only tells you that the PowerShell command itself has been issued correctly, nothing about the script status ...
Michael O'HaraAuthor Commented:
Qlmeo - that run successfully.   Problem is I cannot stay logged in.    Does this result point you to something else in Task Sched?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
So doing exactly the same (stay logged in, but set the task to "whether logged in or not") does stop the script from working?
Most important advice if you need to debug for yourself: Only change one option, switch etc. at one time, and start with something working.

Sadly we see a lot of issues with Task Scheduler, PowerShell and logged-out sessions, and often are not able to locate the culprit, because it depends on the environment. This case here should allow Experts to try to perform some tests themselves. But no promise :p.
I'm willing to bet that "I:" is a mapped network drive.
Replace that with the UNC to the share that it's mapped to, and try again.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michael O'HaraAuthor Commented:
oBda - you are on your game.   Worked like a charm... thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.