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

青春壹個敷衍的年華 提交于 2021-01-27 19:17:58

问题


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:

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""

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

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?


回答1:


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.




回答2:


I also could not launch scripts, after heavy searching nothing helped. No -ExecutionPolicy, no commands, no files and no difference between "" and ''.

I simply put the command I ran in powershell in argument tab: ./scripts.ps1 parameter1 11 parameter2 xx and so on. Now the scheduler works. Program: Powershell.exe start in: C:/location/of/script/



来源:https://stackoverflow.com/questions/62245797/how-to-setup-a-powershell-script-in-windows-task-scheduler-with-admin-permission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!