How to schedule a PS script in task scheduler which copies files from remote computer to local machine?

两盒软妹~` 提交于 2020-04-30 06:33:27

问题


My requirement was to copy certain files from remote machine to the local machine and I have written mapped the folder to the local machine.I use the drive letter of the mapped folder and the script works fine. But when I schedule the task, it throws error that the drive doesn't exists or isn't mapped.

Please find the script below:

$destination = "D:\Script\"
$source = "T:\"
$datefolders = Get-ChildItem $source
$TodayString = (Get-Date).ToString("yyyy-MM-dd")

foreach($folder in $datefolders)
{
    $foldername = $folder.ToString()
    if ($foldername -eq $TodayString)
    {
        $path1 = $source + $foldername + "\*\*.csv"
        Copy-Item -Path $path1 -Destination $destination -Force
        $allfiles = Get-ChildItem "D:\Script\"
        foreach($file in $allfiles)
        {    
            $filename = ([io.fileinfo]$file).basename
        $filepath = $destination + $file
        $connectorfolder = "D:\Script\" + $filename + "\"
            Move-Item -Path $filepath -Destination $connectorfolder -Force
        }
        break
    }
}

Please note that the script works fine when ran directly. Please help me on how I can provide the remote server details which is shown as "T:\" drive in my script.

The error I can see in Event viewer is given below:

Error Message = Could not find the drive 'T:\'. The drive might not be ready or might not be mapped.

Please help.

Regards,

Mitesh Agrawal

来源:https://stackoverflow.com/questions/61291654/how-to-schedule-a-ps-script-in-task-scheduler-which-copies-files-from-remote-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!