Powershell启动进程运行Windows资源管理器不等待

标签 powershell explorer start-process

我有一个 PS 脚本来监视网络中特定服务器列表的日志文件。如果脚本逻辑发现我正在监视的问题,我想通过启动 Windows 资源管理器并等待相关服务器网络文件夹路径来中断脚本进程。

Windows 资源管理器确实显示了请求的文件夹,但 PS 不会等待它关闭。

这是我正在测试的脚本:

# Sample networkfolderpath
$networkfolderpath = '\\server\d$\parent\child'

Start-Process explorer.exe -ArgumentList $networkfolderpath -Wait

仅供引用:我有一个以相同方式设置的 RDP 功能,并且它确实按预期等待。

Start-Process mstsc /v:$computername -Wait  

我现在假设 Windows 资源管理器的行为与其他一些 exe 不同。

我错过了什么?

最佳答案

正如评论中提到的,使用 mstsc 的示例之所以有效,是因为关闭它生成的主窗口和退出进程之间存在一对一的关系。

explorer 不存在这种关系 - 它会在启动时检测到桌面 session 已经有一个 shell,通知它该请求,然后立即退出。

相反,使用Read-Host来阻止脚本的进一步执行,直到用户按下回车键:

# launch folder window
Invoke-Item $networkfolderpath
# block the runtime from doing anything further by prompting the user (then discard the input)
$null = Read-Host "Edit the files, then hit enter to continue..."

关于Powershell启动进程运行Windows资源管理器不等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71022470/

相关文章:

powershell - 获取所有打开的资源管理器窗口的报告

powershell - 有关使用PowerShell Select-String,exiftool(-k)的问题

arrays - Powershell中的数组类型-System.Object []与具有特定类型的数组

powershell - 在 PowerShell 中,如何在文件中定义函数并从 PowerShell 命令行调用它?

windows - 列出所有打开的资源管理器窗口的 Powershell 脚本

c++ - 在资源管理器中打开

powershell - 如何阻止 Powershell 过滤将数组转换为字符串?

PowerShell错误: The Underlying connection was closed

powershell - Autodesk Revit 的启动过程等待问题

powershell - 如果使用 -Wait 参数运行,如何更改 powershell 中进程的优先级?