如何运行PowerShell脚本而不向用户显示窗口或任何其他符号?
换句话说,脚本应该在后台安静地运行,而不需要向用户发出任何信号。
不使用第三方组件的答案可获得额外分数:)
如何运行PowerShell脚本而不向用户显示窗口或任何其他符号?
换句话说,脚本应该在后台安静地运行,而不需要向用户发出任何信号。
不使用第三方组件的答案可获得额外分数:)
当前回答
我认为在运行后台脚本时隐藏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
如果这个答案是帮助你,请在这篇文章中投票给“蓝蛋糕”。
其他回答
我创建了一个小工具,通过原始文件将调用传递给任何你想要启动无窗口的控制台工具:
https://github.com/Vittel/RunHiddenConsole
编译完成后,只需将可执行文件重命名为“<targetExecutableName>w.exe”(附加一个“w”),并将其放在原始可执行文件旁边。 然后你可以用通常的参数调用e.G. powershell .exe,它不会弹出一个窗口。
如果有人知道如何检查创建的进程是否等待输入,我会很高兴包括你的解决方案:)
我认为在运行后台脚本时隐藏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
如果这个答案是帮助你,请在这篇文章中投票给“蓝蛋糕”。
创建一个调用PowerShell脚本的快捷方式,并将Run选项设置为最小化。这将防止窗口闪烁,尽管您仍然会在任务栏上运行脚本的瞬间闪烁。
下面是windows 10中不包含任何第三方组件的工作解决方案。它的工作原理是将PowerShell脚本包装到VBScript中。
第一步:我们需要改变一些windows特性,允许VBScript运行PowerShell,并默认使用PowerShell打开。ps1文件。
-运行并输入“regedit”。单击ok,然后允许它运行。
-粘贴此路径"HKEY_CLASSES_ROOT\Microsoft.PowerShellScript. "1\Shell”并按enter键。
-现在打开右边的条目并将值更改为0。
-以管理员身份打开PowerShell,输入“Set-ExecutionPolicy -ExecutionPolicy remotessigned”,按enter并确认更改“y”,然后输入。
步骤2:现在我们可以开始包装脚本了。
-将Powershell脚本保存为.ps1文件。
-创建一个新的文本文档并粘贴这个脚本。
Dim objShell,objFSO,objFile
Set objShell=CreateObject("WScript.Shell")
Set objFSO=CreateObject("Scripting.FileSystemObject")
'enter the path for your PowerShell Script
strPath="c:\your script path\script.ps1"
'verify file exists
If objFSO.FileExists(strPath) Then
'return short path name
set objFile=objFSO.GetFile(strPath)
strCMD="powershell -nologo -command " & Chr(34) & "&{" &_
objFile.ShortPath & "}" & Chr(34)
'Uncomment next line for debugging
'WScript.Echo strCMD
'use 0 to hide window
objShell.Run strCMD,0
Else
'Display error message
WScript.Echo "Failed to find " & strPath
WScript.Quit
End If
-现在更改文件路径到你的。ps1脚本的位置,并保存文本文档。
-现在右键单击文件并重命名。然后将文件名扩展名更改为.vbs,按enter键,然后单击确定。
完成了!如果你现在打开.vbs,当你的脚本在后台运行时,你应该看不到控制台窗口。
我从c#运行时遇到了这个问题,在Windows 7上,“交互式服务检测”服务在以SYSTEM帐户运行隐藏的powershell窗口时弹出。
使用"CreateNoWindow"参数可以防止ISD服务弹出警告。
process.StartInfo = new ProcessStartInfo("powershell.exe",
String.Format(@" -NoProfile -ExecutionPolicy unrestricted -encodedCommand ""{0}""",encodedCommand))
{
WorkingDirectory = executablePath,
UseShellExecute = false,
CreateNoWindow = true
};