【问题标题】:Run script using Task Scheduler with user context complete silent with no popup or cmd flash使用任务计划程序运行脚本,用户上下文完全静默,没有弹出窗口或 cmd flash
【发布时间】:2016-12-06 13:26:37
【问题描述】:

我尝试使用命令运行脚本

cmd.exe /c Start /min powershell.exe -windowstyle hidden -file <file>.ps1

但是会在几分之一秒内获得一个 CMD 窗口。我需要它完全隐藏运行。

【问题讨论】:

  • 只需运行 PowerShell 脚本,不要使用所有的 starthidden mumbo jumbo,并在任务上启用“无论用户是否登录都运行”复选框。
  • @AnsgarWiechers 的评论应该是答案。

标签: powershell batch-file vbscript scheduled-tasks powershell-3.0


【解决方案1】:

无论用户是否登录,都配置定时任务运行:

并将命令行简化为:

powershell.exe -File "C:\path\to\your.ps1"

这使任务在后台运行,没有可见窗口。

【讨论】:

  • 您可能需要将自己添加到 Backup Operators 组才能工作,因为在您的帐户下在后台运行任务(如果脚本需要更高的权限)将需要 Run Batch 权限。通过将自己添加到 Backup Operators,我解决了与本文略有不同的问题:danblee.com/log-on-as-batch-job-rights-for-task-scheduler
【解决方案2】:

我遇到了这个问题,我可以解决它的唯一方法是使用简单的 VBS 包装器调用 PowerShell 脚本:

https://github.com/gbuktenica/PsRun

http://blog.buktenica.com/run-a-powershell-task-silently/

' SYNOPSIS
'   Run a PowerShell script in the user context without a script window
' EXAMPLE
'   wscript.exe PsRun.vbs MyPsScript.ps1
' AUTHOR
'   Glen Buktenica

Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
    Dim PSRun
    PSRun = "powershell.exe -WindowStyle hidden -ExecutionPolicy bypass -NonInteractive -File " & arg
    objShell.Run(PSRun),0

【讨论】:

  • 隐藏VBS运行的神奇之处在于行尾的0:objShell.Run(PSRun),0
  • Glen Buktenica 的脚本运行良好,但“For”需要一个“Next”。
猜你喜欢
  • 1970-01-01
  • 2020-06-17
  • 1970-01-01
  • 2015-12-25
  • 2017-06-21
  • 2016-08-19
  • 2015-07-22
  • 2014-01-27
  • 2022-01-09
相关资源
最近更新 更多