Task Scheduler - powershell script submitted with drive maps

Hello,

I am new to powershell and have used it to run some file moves which need to happen every 5 minutes. To do this I have a ps1 script file and submit this from Windows task scheduler. In the script I setup the drive map with a “Net Use \drive\tobemapped\ username password”. I then move the files and close the drive map using “Net Use \drive\tobemapped /Delete /Yes”.

This all seems to work fine but I do have one question - when the task scheduler job runs does the drive map only exist within the scheduled job or does the mapping affect the server? so for instance while the job was running and it had mapped the drive could I then access the drive if I was logged onto the server without having to enter any user/password information.

I have not seen any information which states the mapping would only exist within the running jobs session and once the job completes the mapping would be ended.

Do any of you more experienced PS people know if this is the case?

That’s actually not a Powershell issue because you’re using a Windows system command line tool - “net.exe”. You could figure out by yourself. Start a cmd, run your “Net Use \drive\tobemapped\ username password”. Now open another cmd and try to access the share without username and password.

BTW: I’d recommend to use UNC paths instead of mapping drives. You could run the script in the task scheduler with the apropriate account information. That would even avoid to have the account information inside the script. I’d expect that to be much more reliable and robust. :wink:

Drive mappings are user specific so as soon as that user logs off the drive will not be attached.

In other words if you logged on as yourself and map a drive and you remote desktop as another user, that 2nd user will not see the drive that you mapped.

Now if you right code using a service account and map a drive in the code, while that session is running the drive should persist. NET.EXE also has a persistence switch to make drive mappings survive a log off and back on event. However, that is still specific to that user and is a bad idea because as passwords change that mapped drive will lock that users account.

But he’s right none of this relates to PowerShell, still happy to help is I can.

-VERN

Thanks Olaf and Vern - I thought it a powershell question as the script I am running is a powershell script. So while running would the powershell session not be a new independent session and when it completes any drive mapping within it would then not persist? I do have the /Delete /Yes to close it off anyway.

Well … yes and no … :wink: … if you manipulate the environment for the machine it will influence all users. If you manipulate the user environment it will influence only this user.
What’s the actual question you want get an answer for? Why are you asking this?