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?
\\server\share\folder\filename.ext. – Mark Mar 10 '20 at 18:13Local Systemmay not have access to network resources. – Glenn Ferrie Mar 10 '20 at 18:35