Search code examples
windowspowershellgcloudwindows-task-scheduler

How to setup a Powershell Script in Windows Task Scheduler with admin permissions?


I am running the following PowerShell script that creates a Tableau backup and uploads it to Google Cloud Storage using the Windows Task Scheduler.

#Tableau Server backup
&$tsm maintenance backup -f $Backups_file -d -u $User -p $Password


CD "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin"
$backups_folder = "D:\Tableau Server\data\tabsvc\files\backups\" #default backup path for Tableau installation #&$tsm configuration get -k basefilepath.backuprestore
$filename = get-childitem -path $backups_folder -Filter "*.tsbak" | where-object { -not $_.PSIsContainer } | sort-object -Property $_.CreationTime | select-object -last 1 
$fullpath = Join-Path $backups_folder $filename 
gsutil cp $fullpath  gs://my_bucket/backups #upload the latest backup to GCP

I use the command:

powershell -Command  "start-process -verb runAs "powershell " -argumentlist "E:\Tableau\test.ps1""

Which prompts the following message:

Prompt

Which runs the script, creates the backup and uploads it to GCS successfully.

Now I need to automate this script using Windows Task Scheduler, which I did as follows:

Program/script: powershell
Add arguments (optional): -Command  "start-process -verb runAs "powershell " -argumentlist "E:\Tableau\test.ps1""

Command setup

I setup the task to run with highest privileges by the user SYSTEM:

User and privileges

In this scenario, nothing happens.

I tried this other command that creates the backups but doesn't upload to GCS. When I run it manually on CMD using the command powershell -executionpolicy bypass -file 'E:\Tableau\Tableau Backup\test.ps1 I get the following error:

PS D:\Tableau Server\data\tabsvc\files\backups> powershell -executionpolicy bypass -file 'E:\Tableau\Tableau Backup\test
.ps1'
CommandException: Error opening file "file://D:\Tableau Server\data\tabsvc\files\backups\TableauBackup-2020-06-06.tsbak"
: [Errno 13] Permission denied: u'D:\\Tableau Server\\data\\tabsvc\\files\\backups\\TableauBackup-2020-06-06.tsbak'.

So clearly, there's a permissions error when using the gsutil command. When I launch my cmd manually as Administrator, the command runs smoothly.

This last command works smoothly when triggered from a non-administrator elevated CMD but won't work when setup in the Windows Task Scheduler:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -Command  "start-process -verb runAs "powershell " -argumentlist "E:\Tableau\test.ps1""

I also tried this which won't work on the Task Scheduler, will run manually when ran into CMD but will also fail with Permission Denied as the previous command I listed:

powershell.exe -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "E:\Tableau\test.ps1"

How can I setup properly myWindows Task Scheduler to run this script successfully?


Solution

  • This ended up working for me.

    Program/script: powershell
    Add arguments (optional): -ExecutionPolicy Bypass -command "E:\Tableau\test.ps1 2>&1 > E:\Tableau\test_output.txt"
    

    Or in a single command to run from an administrator elevated CMD:

    powershell -ExecutionPolicy Bypass -command "E:\Tableau\test.ps1 2>&1 > E:\Tableau\test_output.txt"
    

    The real issue was that I was running this on a Google Cloud VM and I needed to change the scopes of the VM to have access to all the APIs.

    After I did that, I was able to run the script successfully.