Remote map drive not showing in powershell thred using Task scheduler

Hello,

I have a powershell script that is executing a thread which need to be able to access a remote path (for example : \server_ip\share) which is mapped under some drive letter (for example E:)

I notice that the thread is unable to see (and access) the mapped drive.

I have a small example which i did :

File : test1.ps1

Start-Transcript -OutputDirectory "c:\temp" 
Get-PSDrive

Start-Job -Name "test1" -ScriptBlock {
   $argu = "c:\temp\test2.ps1" 
   Start-process powershell.exe -ArgumentList $argu
}
Start-Sleep -Seconds 20
Stop-Transcript

File : test2.ps1

Start-Transcript -OutputDirectory "c:\temp"
Get-PSDrive
Stop-Transcript

When running the test1.ps1 script using task scheduler , i can see that the thread process is unable to access (or see) the remote drives. From the transcript , i see that the Username + RunAs User are the same as the original script , but still , no shared drives.

I tried checking for user permissions , also running the New-PSDrive from inside the main script did not help . The thread process is still unable to see the shares.

Any help would be much appreciated.

Thanks.

I missed the part about Task Scheduler at first, but I think that’s your problem.
Even though you’re running the script/task with the user credentials that does not mean that it works as a fully logged on user session. The script will create and run in a limited session which does not have access to the mapped share.
If possible I would test to see if it was possible to run the task locally, using only the physical volumes available, at first.
If that works I see two possible ways of dealing with the issue of the remote drive.

  1. Change the path in the script to use the UNC path in stead of the mapped drive letter. As long as the user has both Share and NTFS permissions there should be no issues with that.
  2. Map the drive manually at the top of the script with New-PSDrive cmdlet. Be sure to include proper error handling when doing that so the script stops if the mapping fails for some reason. And I would probably unmap it again at the end of the script with the Remove-PSDrive cmdlet so it cleans up after itself.
1 Like

Hi Iaage,

Thanks for your reply . I am sorry about the Task Scheduler , i edit it in after posting . It took me some time to fully understand your answer.

But , i think i got it and i was able to finally access the drive.

As your first suggestion , i mounted the drive using PSDrive in the main script with username and password and then i was able to access the drive from the thread script using the UNC path → \server_ip\forlder\

It is working now.

Thanks for your support.