我有一个 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/