How to run powershell script using task scheduler in silent/hidden mode?

佐手、 提交于 2020-03-18 05:35:08

问题


I am trying to run powershell script using task scheduler and it pops up powershell cmd window. Is there a way to disable this while running the script? I am on powershell v5.0

I've tried -WindowStyle Hidden but still powershell cmd window pops up.

Any guidance welcome.

Thank you.


回答1:


Since powershell.exe is a console program, you can't execute it normally without its console window appearing (although you can hide it shortly after it starts by using -WindowStyle Hidden, as you have noted).

The trick is to execute powershell.exe itself in a hidden state. One way to do this is by using the WshShell object's Run method to run a PowerShell command line as hidden from inside a WSH script, and execute the WSH script using wscript.exe (which is not a console program, so no console window appears). Example script (JScript):

var wshShell = new ActiveXObject("WScript.Shell");
wshShell.Run('%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -File "C:\\Scripts\\My Script.ps1"', 0, false);

If this script is C:\Scripts\My Launcher.js, you can now run the following command line:

wscript "C:\Scripts\My Launcher.js"

This runs the launcher script without a console, which then runs the PowerShell command in a hidden window (the Run method's second parameter is 0).



来源:https://stackoverflow.com/questions/46808635/how-to-run-powershell-script-using-task-scheduler-in-silent-hidden-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!