1

I wrote a simple ps1 file on C:\ that backs up my dhcp information. It will save that to a shared folder and then delete the oldest after it gets to 6: $date=Get-Date -Format 'MM-dd-yyyy' $file="S:\DHCPBak${date}.txt" netsh dhcp server export $file all if ((Get-ChildItem S:\ -File | Measure-Object).count -gt 5){ Get-ChildItem S:\ | Sort-Object -property lastwritetime | select -first 1 | Remove-Item }

I wanted this code to run on the weekend when everyone is probably logged out.

This code works perfectly when run from inside powershell, running powershell.exe with args, and when task scheduler runs it with the "Run only when user is logged on" enabled.

However, after I click "Run whether user is logged on or not", the program will run successfully, but no files will appear in the directory. The History tab will show that the task is completed, no errors. I can switch it back to "only when user is logged on" and run it to receive my backup file.

I know that the backup file will not replicate, so I've been careful to delete it between tests, the script will just not work properly after I select "logged on or off". Is there a scope mismatch that I'm missing somewhere that is saving these in a different place or is Task Scheduler just unhappy?

Connor
  • 11
  • 3
  • 1
    You're mapped drive won't be available (unless you also map it). Try UNC paths \\server\share\folder\filename.ext. – Mark Mar 10 '20 at 18:13
  • I second @Mark 's response. your "S" drive mapping existing in the context of your user session, but when you run "whether user is logged in or not", then you are in a service process. you might actually run into access issues depending on which service account is used. Local System may not have access to network resources. – Glenn Ferrie Mar 10 '20 at 18:35

1 Answers1

0

After digging through some more questions on here I ran across Windows 10 task scheduler won't run any exe if set to "run whether user is logged in or not". LPChip left a comment saying that SYSTEM doesn't have access to network share or resources. This has to be what's causing my problem.

Connor
  • 11
  • 3
  • Last comment for anyone who found this question with a similar problem I had. while SYSTEM cannot access shared folders, it can access UNC paths to the share. Just change the file path in the script to \\server\path\to\share to make it work again. – Connor Mar 10 '20 at 18:30