Powershell 在启动时使用 ScheduledJob 以管理员权限运行作业 [英] Powershell run job at startup with admin rights using ScheduledJob
问题描述
为了减轻我的一些工作,我创建了一个 powershell 脚本,它需要:
To ease some of my work I have created a powershell script which needs to :
- 在启动时运行.
- 使用管理员权限运行,因为它必须写入 c:\program files 文件夹.
我使用 powershell 创建了启动服务,如下所示:
I created the startup service using powershell like this :
function MakeStartupService
{
Write-Host "Adding script as a startup service"
$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:15
Try
{
Register-ScheduledJob -Trigger $trigger -FilePath "absolute_path" -Name "Job-name" -EA Stop
}
Catch [system.exception]
{
Write-Host "Looks like an existing startup service exists for the same. Overwriting existing job"
Unregister-ScheduledJob "Job-name"
Register-ScheduledJob -Trigger $trigger -FilePath "absolute_path" -Name "Job-name"
}
}
该作业已成功注册为启动服务,并且在任务调度程序中可见.如果我使用 Start-Job -DefinitionName Job-name
或通过从 Task Scheduler 中右键单击来启动它,它可以正常工作,但在 Windows 启动时它不会启动.
The job is registered as a startup service successfully and is visible inside task scheduler. If I start it using Start-Job -DefinitionName Job-name
or by right clicking from Task Scheduler, it works fine but it doesn't start when windows starts.
目前我正在我的个人 Windows 10 系统上对此进行测试,并在另一个 Windows 10 系统中进行了检查,但该行为仍然是名称.我正在附上这项工作的任务计划程序窗口的屏幕截图.
Currently I am testing this on my personal Windows 10 system, and have checked in another windows 10 system but the behavior remained name. I am attaching screenshot of task scheduler window for this job.
抱歉,如果这个问题听起来重复或愚蠢(我是 powershell 的初学者),但相信我,我在网上找到的所有解决方案都无法解决这个问题.
Sorry if this questions sounds repeated or dumb (I am a beginner in powershell), but believe me, none of the solutions I found online worked for this.
提前致谢!!
推荐答案
这是我使用的已经在生产中的代码.如果它对您不起作用,那么您的系统肯定有其他问题.
This is code that is already in production that I use. If it does not work for you, you must have something else going on with your system.
function Invoke-PrepareScheduledTask
{
$taskName = "UCM_MSSQL"
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
if ($task -ne $null)
{
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
}
# TODO: EDIT THIS STUFF AS NEEDED...
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-File "C:\Invoke-MYSCRIPT.ps1"'
$trigger = New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:00:30
$settings = New-ScheduledTaskSettingsSet -Compatibility Win8
$principal = New-ScheduledTaskPrincipal -UserId SYSTEM -LogonType ServiceAccount -RunLevel Highest
$definition = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -Settings $settings -Description "Run $($taskName) at startup"
Register-ScheduledTask -TaskName $taskName -InputObject $definition
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
# TODO: LOG AS NEEDED...
if ($task -ne $null)
{
Write-Output "Created scheduled task: '$($task.ToString())'."
}
else
{
Write-Output "Created scheduled task: FAILED."
}
}
这篇关于Powershell 在启动时使用 ScheduledJob 以管理员权限运行作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!