无法通过任务计划程序通过 powershell 启动程序

无法通过任务计划程序通过 powershell 启动程序

我有一个简单的 powershell 脚本,可以检测程序是否正在运行,如果没有运行,则启动该程序。当我右键单击并“使用 PowerShell 运行”脚本时,它可以正常工作,但将 .ps1 的默认程序更改为 powershell 并双击它则不行。此外,它在任务计划程序中不起作用。作为参考,这是我的脚本:

$filebound = get-process FileBound.Enterprise.ChainEditor -ErrorAction SilentlyContinue
if($filebound -eq $null)
{
    start-process -filepath "C:\Program Files (x86)\FileBound\Importer Professional\FileBound.Enterprise.ChainEditor.exe"
}

我找不到正确的方法来论证任务计划程序中的操作以使其工作。运行任务时,电源外壳屏幕出现并消失,但应用程序不运行(与双击的行为相同)。

这是我的任务信息:

Run only when user is logged in and run with highest priveleges checked.  Configured for Windows 10 (it's a windows 10 vm).
Triggers at startup and repeats every 30 minutes indefinitely.  Enabled is checked
Actions starts a program (powershell)
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
with argument (stolen from someone else) -command "& C:\Filebound\Test.ps1" -NonInteractive
Only Condition checked is Wake computer to run task.
Settings are Allow Task to run on demand and Run task as soon as possible are checked.

任何帮助都将受到赞赏。

答案1

搞清楚了。这与任务计划程序中的参数有关。一旦我将其设置为: -ExecutionPolicy Bypass C:\Filebound\Test.ps1一切正常。

附注:我必须将任务调整为“当计算机空闲时”作为触发器而不是“在启动时”以使其更加可靠。

答案2

您可以使用 PowerShell 将执行策略设置为不受限制,这样您就不需要-ExecutionPolicy Bypass每次运行 ps1 脚本时都指定参数。

您可以打开 PowerShell,但此方法需要管理员权限,请使用以下方法打开提升的 PowerShell:

Win+ →R类型PowerShellCtrl++ShiftEnter

然后将其粘贴到 PowerShell 窗口中:

New-Item -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell" -ErrorAction SilentlyContinue | Out-Null
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell" -Name EnableScripts -Type DWord -Value 1
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell" -Name ExecutionPolicy -Type String -Value Unrestricted

然后您不需要绕过执行策略来运行脚本。

这相当于更改此组策略:

Win+ R→类型gpedit.mscEnter

导航:

"Local Computer Policy"→"Computer Configuration"→"Administrative Templates"→"Windows Components"→"Windows PowerShell"

在左侧面板中,单击“Windows PowerShell”。

在右侧双击“开启脚本执行”,点击“启用”,在“执行策略”下拉列表中选择“允许所有脚本”,点击Apply,然后点击OK

相关内容