如何在具有管理员权限的Windows Task Scheduler中设置Powershell脚本? [英] How to setup a Powershell Script in Windows Task Scheduler with admin permissions?
问题描述
我正在运行以下PowerShell脚本,该脚本创建一个Tableau备份,并使用Windows Task Scheduler将其上传到Google Cloud Storage.
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
我使用以下命令:
powershell -Command "start-process -verb runAs "powershell " -argumentlist "E:\Tableau\test.ps1""
其中会提示以下消息:
运行脚本,创建备份并将其成功上传到GCS.
Which runs the script, creates the backup and uploads it to GCS successfully.
现在,我需要使用Windows Task Scheduler自动执行此脚本,如下所示:
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""
我将任务设置为以用户SYSTEM的最高特权运行:
I setup the task to run with highest privileges by the user SYSTEM:
在这种情况下,什么也没有发生.
In this scenario, nothing happens.
我尝试了另一个创建备份但不上传到GCS的命令.当我使用命令 powershell -executionpolicy旁路-file'E:\ Tableau \ Tableau Backup \ test.ps1
在CMD上手动运行它时,出现以下错误:
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'.
很明显,使用gsutil命令时出现权限错误.当我以管理员身份手动启动cmd时,命令运行平稳.
So clearly, there's a permissions error when using the gsutil command. When I launch my cmd manually as Administrator, the command runs smoothly.
从非管理员提升的CMD触发后,此最后一个命令可以正常运行,但在Windows Task Scheduler中进行设置时将不起作用:
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""
我还尝试了此在Task Scheduler上不起作用,将在遇到CMD时手动运行,但由于我上面列出的上一个命令,权限被拒绝也会失败:
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"
如何正确设置myWindows Task Scheduler才能成功运行此脚本?
How can I setup properly myWindows Task Scheduler to run this script successfully?
推荐答案
这最终对我有用.
Program/script: powershell
Add arguments (optional): -ExecutionPolicy Bypass -command "E:\Tableau\test.ps1 2>&1 > E:\Tableau\test_output.txt"
或通过单个命令从管理员提升的CMD运行:
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"
真正的问题是我正在Google Cloud VM上运行此软件,并且需要更改VM的范围才能访问所有API.
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.
这篇关于如何在具有管理员权限的Windows Task Scheduler中设置Powershell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!