r/PowerShell • u/Iknuf • Apr 15 '26
Question Copying fails when run by Task Scheduler
So my script is intended to export a Hyper-V hosted VM and copy the export over to a NAS. The address for copying should already be the network address, as it does not start with a drive letter - the format is
"\\192.168.10.10\Backups\Exports"
(IP address and directory names are changed, but the format is identical)
Exporting is never the issue, but the copy always fails when I run the script via the MS Task Scheduler, while working perfectly fine when run over Powershell for debugging (I am running it by entering "powershell.exe -ExecutionPolicy Bypass -File [path]" into PS, to make it most similar to the way it is executed by the Task Scheduler)
The Task is already being executed by SYSTEM (NT AUTHORITY\SYSTEM) and with highest privileges. Disk space is not the issue, as I ensured it is sufficient multiple times and after multiple tests the issue only occurs when run by the Scheduler
EDIT: I was able to narrow down the error to the Scheduler not having NAS access - now I just don't know how to add the credentials to my script
Any ideas or help?
6
u/philixx93 Apr 15 '26
How is the NAS supposed to know your local SYSTEM account? Run it in the context of a user, that is recognized by the NAS, or provide credentials. If you are trying to use a mount of a user, this will fail too because on Windows network mounts live in the user’s context.
18
u/TrippTrappTrinn Apr 15 '26
The system account is local to the server running the script and is unknown to the NAS, so it does not have access.
1
u/SVD_NL Apr 15 '26
Use New-PSDrive to mount the NAS with valid creds.
2
u/purplemonkeymad Apr 15 '26
How are you authenticating?
Do you have an ad domain? If yes, you'll want to use fqdn so that kerberos is used, and make sure the computer account has access.
If not you'll probably have to use New-SmbMapping in your script to provide a credential to the connection.
2
u/Secret_Account07 Apr 15 '26
As others have said sounds like a permission issue.
If it were me I’d use a vaulted service account for this.
I hate when folks creates all these one-off accounts for a job then 6 months from now after a security event I gotta figure out what some random local system accounts are or some reused AD account.
1
u/ararag Apr 15 '26
If source machine and destination nas are domain joined you might want to use the source machine's ad account for this. On the destination end, you need to assign permission to the AD machine object of the machine running the scheduled task. Then it will work.
1
u/pigers1986 Apr 15 '26
wrong account doing job ..
mount target path as another user
1
u/digiden Apr 18 '26
Use Start-Transcript -Path "C:\logs.txt" to begin recording and Stop-Transcript to end it. This captures everything written to the console.
3
u/[deleted] Apr 15 '26
[removed] — view removed comment