
プログラムが実行中かどうかを検出し、実行中でない場合はプログラムを起動する簡単な 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"
}
タスク スケジューラでアクションを引数にして動作させる適切な方法が見つかりません。タスクを実行すると、Power Shell 画面が表示されて消えますが、アプリケーションは実行されません (ダブルクリックと同じ動作)。
私のタスク情報は次のとおりです:
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
-ExecutionPolicy Bypass
PowerShell を使用して実行ポリシーを無制限に設定すれば、 ps1 スクリプトを実行するたびにパラメーターを指定する必要がなくなります。
PowerShell を開くことはできますが、この方法には管理者権限が必要です。管理者特権で PowerShell を開くには、次の操作を実行します。
Win+ R→タイプPowerShell
→ Ctrl+ 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.msc
→Enter
案内する:
"Local Computer Policy"→"Computer Configuration"→"Administrative Templates"→"Windows Components"→"Windows PowerShell"
左側のパネルで、「Windows PowerShell」をクリックします。
右側で、「スクリプトの実行を有効にする」をダブルクリックし、「有効」をクリックして、「実行ポリシー」ドロップダウン リストで「すべてのスクリプトを許可する」を選択し、 をクリックして、ApplyをクリックしますOK。