kmwoley / restic-windows-backup

Powershell scripts to run Restic backups on Windows
MIT License
330 stars 68 forks source link

powershell execution policy is blocking the scheduled task #27

Closed scelfo closed 3 years ago

scelfo commented 3 years ago

I got this all running if I execute the backup.ps1 script directly in powershell. However, the task in task scheduler was not running and it was really frustrating me.

I have a normal windows 10 installation and my task was running as SYSTEM user, the defaults set up by this project.

I ended up installing psexec so I could run a command problem as the SYSTEM users (instructions: https://www.gabsoftware.com/tips/run-cmd-or-any-process-as-system-account-on-windows/) and was able to determine that my problem was caused by execution policies which were set as Restricted (documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.1).

I poked around, found https://blog.netspi.com/15-ways-to-bypass-the-powershell-execution-policy/ and was able to make everything work by simply adding "-ExecutionPolicy Bypass" to the arguments of the restic backup task in task scheduler. I'm filing this to document my problem and fix and I might send a pull request to add "-ExecutionPolicy Bypass" to the task created by install.ps1.

kmwoley commented 3 years ago

Thanks for reporting this. I think the reason I wasn't encountering this is because I have my execution policy set to RemoteSigned. I'll include this in the next release.

scelfo commented 3 years ago

No problem, happy to help. I'm running your script on a few very clean windows installations with the bare minimum needed to get up and running so hopefully my experience is common.

I have a few other tweaks in the backup script but they aren't useful to others. Notably I'm using scoop to install restic based on https://restic.readthedocs.io/en/latest/020_installation.html#windows and I disable the cleanup logic in the backup script since I'm managing that elsewhere.