如何在Powershell中创建新窗口并将UseShellExecute设置为false? [英] How to create a new window and set UseShellExecute false in powershell?

查看:75
本文介绍了如何在Powershell中创建新窗口并将UseShellExecute设置为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求有点奇怪,我在Powershell的多线程中遇到了一个奇怪的卡住问题.因此,我想创建一个新窗口,而不使用Shell Execute.但是我不能用下面的代码来实现,窗口不会显示.$ approot是桌面,在start.bat中,只需执行"dir/s.\"我希望目录结果显示在另一个窗口中,而不是在该窗口中执行此脚本,并且我不希望使用shell执行.

The requirement is a bit strange, I've encountered a strange stuck problem in multi-thread in powershell. So I want to create a new window and don't use shell Execute. But I cannot make it with below code, the window doesn't show up. $approot is desktop, in start.bat, just do "dir /s .\" I want the dir result shows in another window instead of the window execute this script, and I don't want use shell execute.

$startInfo = New-Object system.Diagnostics.ProcessStartInfo
$startInfo.UseShellExecute = $false
$startinfo.FileName = $env:ComSpec
$startInfo.CreateNoWindow = $false
$startInfo.Arguments = "/c cd /d $AppRoot & call start.bat"
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null

推荐答案

如果将 .UseShellExecute 设置为 $ False ,则无法在窗口;这样, .CreateNoNewWindow 属性的值就会有效地被忽略.

If you set .UseShellExecute to $False, you cannot start your command in a new window; the value of the .CreateNoNewWindow property is then effectively ignored.

因此,您也可以使用 Start-Process ,它将 .UseShellExecute 保留为默认值 $ True [1] .

Therefore, you may as well use Start-Process, which leaves .UseShellExecute at its default, $True[1].

Start-Process $env:ComSpec -Args '/k', "cd /d `"$AppRoot`" & call start.bat"

为养成良好的习惯,将 $ AppRoot 括在嵌入式双引号中(转义为`")以正确处理包含空格的路径,虽然对于 cmd.exe cd 而言并非绝对必要,但实际上与所有其他命令/程序一样.

To promote good habits, $AppRoot is enclosed in embedded double quotes (escaped as `") to properly deal with paths containing spaces. While this is not strictly necessary with cmd.exe's cd, it is with virtually all other commands / programs.

请注意,我使用的是/k 而不是/c 作为 cmd.exe ( $ env:ComSpec )开关,以确保新窗口保持打开状态.

Note that I'm using /k rather than /c as the cmd.exe ($env:ComSpec) switch to ensure that the new windows stays open.

如果必须将 .UseShellExecute 设置为 $ False ,请使用 conhost.exe 显式创建一个新窗口(要求Windows 10):

If setting .UseShellExecute to $False is a must, use conhost.exe to explicitly create a new window (requires Windows 10):

$process = New-Object System.Diagnostics.Process
$process.startInfo.UseShellExecute = $false
$process.startinfo.FileName = 'conhost.exe'
$process.startInfo.Arguments = "cmd /k cd /d `"$AppRoot`" & call start.bat"
$null = $process.Start()

在Windows的其他版本上,您可以使用P/Invoke调用直接使用

On other flavors / versions of Windows, you may be able to use P/Invoke to call the CreateProcess() Windows API function directly, using the CREATE_NEW_CONSOLE flag.

[1]
*请注意,某些 Start-Process 参数,例如 -NoNewWindow -Credential ,要求将 .UseShellExecute 设置为设置为 $ False ,在这种情况下,幕后将使用 CreateProcess() WinAPI函数(而不是 ShellExecute()).>*还请注意,在.NET Core 中,默认值为 $ False ,但是PowerShell Core的 Start-Process 仍默认为 $ True 以匹配Windows PowerShell的行为.

[1]
* Note that certain Start-Process parameters, such as -NoNewWindow and -Credential, require that .UseShellExecute be set to $False, in which case the CreateProcess() WinAPI function is used (rather than ShellExecute()) behind the scenes.
* Also note that in .NET Core the default is $False, but PowerShell Core's Start-Process still defaults to $True to match Windows PowerShell's behavior.

这篇关于如何在Powershell中创建新窗口并将UseShellExecute设置为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆