R Saudlach
R Saudlach

Reputation: 388

Cannot copy item using mapped drive in Scheduler

I have a simple PowerShell script that copies a file from a mapped network drive, if it's modified in past 1 day.

$source = "Z:\\"
$target = "E:\target"
$files = get-childitem $source

foreach ($file in $files) {
  if($file.LastWriteTime -ge (get-date).AddDays(-1)) {
     Copy-Item $file.FullName $target
  }
}

This script runs fine if I manually execute it.

If I try to use a scheduled task, the copy does not run. I confirmed the script is running by having it make a directory.

If I instead copy from a local drive instead of a network drive, the script runs fine with a scheduled task.

Schedule Task is running as an Admin Account.

Script copying file from network drive runs fine manually but not via scheduled task. Script runs fine as task if copying from local but not network drive.

Any ideas?

Upvotes: 1

Views: 1594

Answers (2)

soundstripe
soundstripe

Reputation: 1474

Map the drive as a temporary PowerShell drive...add the following as the first line of the script

New-PSDrive -Name Z -PSProvider FileSystem -Root \\server\sharename

Upvotes: 1

Bill_Stewart
Bill_Stewart

Reputation: 24555

Try specifying the full UNC path rather than a network drive. (Network drives are a per-user configuration item.)

Upvotes: 3

Related Questions