無法透過任務計劃程序透過 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+ Shift+Enter

然後將它們貼到 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

相關內容