[英]Run Scheduled PowerShell script as admin
I create a windows task using admin PowerShell Register-ScheduledJob
.我使用管理员PowerShell
Register-ScheduledJob
创建了一个 windows 任务。
It works fine when calling it directly by doing Start-Job
.通过执行
Start-Job
直接调用它时效果很好。 But the job triggers execute it without elevated permissions.但是作业触发器在没有提升权限的情况下执行它。 How can I create a task/job on windows ( using admin PowerShell) where the - scheduled script itself - runs as admin too.
如何在 windows(使用管理员 PowerShell)上创建任务/作业,其中 - 计划脚本本身 - 也以管理员身份运行。 On Linux imagine: I edit root crontab.
在 Linux 上想象:我编辑根 crontab。
This is what I tried so far, nothing worked for me (popup does not show):这是我到目前为止所尝试的,对我没有任何帮助(弹出窗口不显示):
$min = New-TimeSpan -Minute 30
$opts = New-ScheduledJobOption -RunElevated -RequireNetwork
Register-ScheduledJob -Name msaccess_invita_gmbh_hook -ScheduledJobOption $opts
-RunNow -RunEvery $min -ScriptBlock {
Start-Process powershell -Verb runAs -ArgumentList
"Add-Type -AssemblyName PresentationCore,PresentationFramework;
[System.Windows.MessageBox]::Show('whatup')"
}
Continuing from my comments, here is a full ST creation:继续我的评论,这是一个完整的 ST 创作:
https://duckduckgo.com/?q=powershell+create+scheduled+task+admin+credentials&t=h_&ia=web https://duckduckgo.com/?q=powershell+create+scheduled+task+admin+credentials&t=h_&ia=web
Example:例子:
$Action = New-ScheduledTaskAction -Execute 'C:\Windows\System32\WindowsPowerShellv1.0\powershell.exe' -Argument "-NonInteractive -NoLogo -NoProfile -File 'C:\scripts\PowerShellscript.ps1'" -WorkingDirectory 'C:\scripts'
$Trigger = New-ScheduledTaskTrigger -RandomDelay (New-TimeSpan -Minutes 5) -AtStartup
$Settings = New-ScheduledTaskSettingsSet -DontStopOnIdleEnd -RestartInterval (New-TimeSpan -Minutes 1) -RestartCount 10 -StartWhenAvailable
$Settings.ExecutionTimeLimit = 'PT0S'
$SecurePassword = $password = Read-Host -AsSecureString
$UserName = 'svc_account'
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword
$Password = $Credentials.GetNetworkCredential().Password
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
$Task | Register-ScheduledTask -TaskName 'ScheduledTaskName' -User 'svc_account' -Password $Password
Update, as per our comment exchange.更新,根据我们的评论交流。
Event with use schtasks.exe, to run with a different identity/creds, you must pass them in on creation.使用 schtasks.exe 的事件,要以不同的身份/凭据运行,您必须在创建时传递它们。
# Create a task to run at 11 pm every weekday
SCHTASKS /Create /SC weekly /D MON,TUE,WED,THU,FRI /TN MyDailyBackup /ST 23:00 /TR c:\backup.cmd /RU MyDomain\MyLogin /RP MyPassword
If it is to run as the current user, then that is the default.如果要以当前用户身份运行,那么这是默认设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.