Tho*_*att 110 windows powershell scripting batch-file silent
如何在不向用户显示窗口或任何其他标志的情况下运行PowerShell脚本?
换句话说,脚本应该在后台安静地运行而不向用户发出任何信号.
对于不使用第三方组件的答案的额外信用:)
ste*_*tej 114
您可以像这样运行它(但这会显示一段时间的窗口):
PowerShell.exe -windowstyle hidden { your script.. }
Run Code Online (Sandbox Code Playgroud)
或者您使用我创建的帮助文件来避免名为PsRun.exe的窗口.您可以在PowerShell中下载源代码和exe文件使用WinForm GUI运行预定任务.我将它用于计划任务.
编辑:正如Marco所说,这个-windowstyle参数仅适用于V2.
ax.*_*ax. 17
您可以使用PowerShell社区扩展并执行以下操作:
start-process PowerShell.exe -arg $pwd\foo.ps1 -WindowStyle Hidden
Run Code Online (Sandbox Code Playgroud)
您也可以使用VBScript执行此操作:http://blog.sapien.com/index.php/2006/12/26/more-fun-with-scheduled-powershell/
安排隐藏的PowerShell任务(Internet存档)
通过预定的PowerShell(Internet Archive)获得更多乐趣
(通过这个论坛帖子.)
Yus*_*sha 16
我遇到了同样的问题.我发现,如果你去的任务在任务计划程序运行的powershell.exe脚本,你可以点击" 运行用户是否登录或不 ",并且任务运行时,将永远不会显示PowerShell窗口.
And*_*wry 13
这是一种不需要命令行参数或单独的启动器的方法.它不是完全不可见的,因为窗口确实在启动时显示.但它很快消失了.如果您想通过双击资源管理器或通过"开始"菜单快捷方式(当然包括"启动"子菜单)来启动脚本,我认为这是最简单的方法.我喜欢它是脚本本身代码的一部分,而不是外部代码.
把它放在脚本的前面:
$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
Run Code Online (Sandbox Code Playgroud)
Ada*_*lor 13
这是一个单行:
mshta vbscript:Execute("CreateObject(""Wscript.Shell"").Run ""powershell -NoLogo -Command """"& 'C:\Example Path That Has Spaces\My Script.ps1'"""""", 0 : window.close")
Run Code Online (Sandbox Code Playgroud)
尽管这可能会非常短暂地闪烁一个窗口,但这种情况应该很少发生。
Gar*_*ric 10
ps1 也隐藏在任务计划程序和快捷方式中
mshta vbscript:Execute("CreateObject(""WScript.Shell"").Run ""powershell -ExecutionPolicy Bypass & 'C:\PATH\NAME.ps1'"", 0:close")
Run Code Online (Sandbox Code Playgroud)
答案-WindowStyle Hidden很好,但窗口仍然会闪烁。
通过cmd /c start /min "".
您的机器或设置可能不同,但对我来说效果很好。
cmd /c start /min "" powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\Users\username\Desktop\test.ps1"
Run Code Online (Sandbox Code Playgroud)
cmd /c start /min "" powershell -WindowStyle Hidden -ExecutionPolicy Bypass -Command ". 'C:\Users\username\Desktop\test.ps1'; -Arg1 'Hello' -Arg2 ' World'"
Run Code Online (Sandbox Code Playgroud)
cmd /c start /min "" powershell -WindowStyle Hidden -ExecutionPolicy Bypass -Command ". 'C:\Users\username\Desktop\test.ps1'; Get-Test -stringTest 'Hello World'"
Run Code Online (Sandbox Code Playgroud)
3. 用函数和参数调用文件的Powershell 内容是:
function Get-Test() {
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true, HelpMessage = 'The test string.')]
[String]$stringTest
)
Write-Host $stringTest
return
}
Run Code Online (Sandbox Code Playgroud)
如果您需要在 Task Scheduler 中运行此程序,则将其%comspec%作为程序/脚本调用,然后作为参数调用上述文件的代码。
我在使用c#运行时出现此问题,在Windows 7上,当运行隐藏的PowerShell窗口作为SYSTEM帐户时,"Interactive Services Detection"服务弹出.
使用"CreateNoWindow"参数阻止了ISD服务弹出它的警告.
process.StartInfo = new ProcessStartInfo("powershell.exe",
String.Format(@" -NoProfile -ExecutionPolicy unrestricted -encodedCommand ""{0}""",encodedCommand))
{
WorkingDirectory = executablePath,
UseShellExecute = false,
CreateNoWindow = true
};
Run Code Online (Sandbox Code Playgroud)
小智 5
我认为,运行后台脚本时隐藏PowerShell控制台屏幕的最佳方法是此代码(“ Bluecakes ”答案)。
我将此代码添加到需要在后台运行的所有PowerShell脚本的开头。
# .Net methods for hiding/showing the console in the background
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
function Hide-Console
{
$consolePtr = [Console.Window]::GetConsoleWindow()
#0 hide
[Console.Window]::ShowWindow($consolePtr, 0)
}
Hide-Console
Run Code Online (Sandbox Code Playgroud)
如果此答案对您有帮助,请在本文中的投票中对“ Bluecakes”进行投票。
这是一个控制控制台各种状态的有趣演示,包括最小化和隐藏。
Add-Type -Name ConsoleUtils -Namespace WPIA -MemberDefinition @'
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'@
$ConsoleMode = @{
HIDDEN = 0;
NORMAL = 1;
MINIMIZED = 2;
MAXIMIZED = 3;
SHOW = 5
RESTORE = 9
}
$hWnd = [WPIA.ConsoleUtils]::GetConsoleWindow()
$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.MAXIMIZED)
"maximized $a"
Start-Sleep 2
$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.NORMAL)
"normal $a"
Start-Sleep 2
$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.MINIMIZED)
"minimized $a"
Start-Sleep 2
$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.RESTORE)
"restore $a"
Start-Sleep 2
$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.HIDDEN)
"hidden $a"
Start-Sleep 2
$a = [WPIA.ConsoleUtils]::ShowWindow($hWnd, $ConsoleMode.SHOW)
"show $a"
Run Code Online (Sandbox Code Playgroud)
为了方便命令行使用,有一个简单的包装应用程序:
https://github.com/stax76/run-hidden
命令行示例:
run-hidden powershell -command calc.exe
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
314052 次 |
| 最近记录: |