- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标是运行多个进程并保存它们的 ProcessName
和 Id
供以后使用。这是我的代码
[System.Collections.ArrayList]$startedProcesses = @()
$processStatus = Start-Process -FilePath notepad.exe -passthru
Start-Sleep 1
$startedProcesses.add($processStatus)
$processStatus = Start-Process -FilePath calc.exe -passthru
Start-Sleep 1
$startedProcesses.add($processStatus)
echo $startedProcesses
PS C:\Users\wakatana\Desktop\> .\so_question0.ps1
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
235 15 3408 14112 0.08 24812 2 notepad
0 0 0 0.13 21460
[System.Collections.ArrayList]$startedProcesses = @()
与
$startedProcesses = New-Object System.Collections.Generic.List[System.ComponentModel.Component]
但我以同样的结果结束。
calc
在
ProcessName
下?如果我删除
Start-Sleep 1
然后我得到了
calc
在
ProcessName
下.这是为什么?这是启动应用程序的正确方式还是我做错了什么?我的操作系统:Windows 10 家庭版
最佳答案
您的问题与 Start-Sleep
的使用无关.
相反,问题在于,从 Windows 10 开始, calc.exe
只是最终启动的进程的 stub 可执行文件, stub 进程在启动真正的可执行文件后立即退出 .
如果您删除 Start-Sleep
Start-Process
之间的通话和 echo
调用,包含在 $startedProcesses
中的 stub 进程对象通常确实反射(reflect)了 stub 可执行文件的名称 - calc.exe
- 在 ProcessName
当时的专栏,由于仍然活着(尽管只是短暂的),但您仍然无法跟踪真正的可执行文件的进程生命周期并通过该对象退出代码。
真正的可执行文件的名字是Calculator.exe
,其确切路径包含完整的 AppX 包名称(包名称、版本号和发布者 ID):
例如,在启动 calc.exe
之后, (Get-Process Calculator).Path
产生类似的东西:
C:\Program Files\WindowsApps\Microsoft.WindowsCalculator_10.1908.0.0_x64__8wekyb3d8bbwe\Calculator.exe
但是,即使您知道该路径,也不能使用它直接启动 Calculator。
Start-Process ms-calculator:
不幸的是,从 PowerShell (Core) 7.2/Windows PowerShell v5.1 开始,
添加 -PassThru
使用 Start-Process
调用 AppX 包 URL导致错误 而不是返回一个代表已启动进程的对象——尽管 Calculator 仍会启动;
这同样适用于 -Wait
:
# Launches Calculator, but doesn't return a process object
# and reports an error instead:
PS> Start-Process ms-calculator: -PassThru
Start-Process : This command cannot be run completely because the system cannot find all the information required.
...
该问题已在
GitHub issue #10996 中报告。 .
# Invoke the *stub executable* synchronously, so that
# the real executable has already been launched by the time
# the call returns.
Start-Process -Wait -FilePath calc.exe
# Now get the real process object, named 'Calculator'.
# The most recently launched instance is used.
$processStatus = (Get-Process -Name Calculator | Sort-Object TotalProcessTime)[0]
注意:此方法并非万无一失,因为假设另一个进程可以同时启动一个 Calculator 实例,但在实践中应该足够健壮。
*Calculator*
,见
this answer .
关于powershell - Start-Process 不能很好地与 Start-Sleep 配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58721461/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 5年前关闭。 Improve t
我是一名设计老师,试图帮助学生应对编程挑战,所以我编码是为了好玩,但我不是专家。 她需要找到 mode (最常见的值)在使用耦合到 Arduino 的传感器的数据构建的数据集中,然后根据结果激活一些功
我正在开发一个应用程序,该应用程序提供 CPU 使用率最高的 5 个应用程序名称。目前,我通过以下代码获得了排名前 5 的应用程序: var _ = require('lodash');
互联网上很少有例子涉及这个问题的所有三个问题——即 set-process-sentinel ; set-process-filter ;和 start-process . 我尝试了几种不同的方法来微
如 this post 中所述,在 C# 中有两种调用另一个进程的方法。 Process.Start("hello"); 和 Process p = new Process(); p.StartInf
我试图让我的桨从白色变为渐变(线性),并使球具有径向渐变。感谢您的帮助!您可以在 void drawPaddle 中找到桨的代码。 这是我的目标: 这是我的代码: //球 int ballX = 50
考虑:流程(a)根据我的文字: A process is first entered at the time of simulation, at which time it is executed u
我真的希望 Processing 有用于处理数组的 push 和 pop 方法,但由于它没有,我不得不试图找出删除数组中特定位置的对象的最佳方法。我相信这对很多人来说都是基本的,但我可以使用一些帮助,
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
以编程方式,我如何确定 Windows 10 中的 3 个类别 应用 后台进程 Windows 服务 就像任务管理器一样? 即我需要一些 C# 代码,我可以确定应用程序列表与后台进程列表。检查 Win
当我导入 node:process它工作正常。但是,当我尝试要求相同时,它会出错。 这工作正常: import process from 'node:process'; 但是当我尝试要求相同时,它会引
我正在上一门使用处理的类(class)。 我在理解 map() 函数时遇到问题。 根据它的文档( http://www.processing.org/reference/map_.html ): Re
我试图执行: composer.phar update 并收到: Fatal error: Allowed memory size of 94371840 bytes exhausted (tried
给定一堆二维图像,如何使用 Processing/Processing.js 产生体积渲染效果? 目前我的想法是使用 java(类似于 imageJ)进行体积渲染 -> 获取体积渲染图像的面作为单独的
这是代码示例 var startInfo = new ProcessStartInfo { Arguments = commandStr, FileName = @"C:\Window
当我在 Processing(草图 > 导入库 > 添加库)中添加库时,它安装在哪里? 最佳答案 它们安装在您的 中速写本位置 . 您可以通过转到"file">“首选项”来查看和更改您的速写本位置。草
无聊的好奇... 我正在查看当前进程的一些属性: using(Process p = Process.GetCurrentProcess()) { // Inspect properties
我正在尝试在同一页面上运行多个草图。 初始化脚本指定: /* * This code searches for all the * in your page and loads each scrip
Process.Kill 后是否需要使用 Process.WaitForExit? 如果调用进程在调用 Process.Kill 后立即退出怎么办? 这会导致 Process.Kill 失败吗? 编辑
我尝试使用处理从麦克风获取频率。我混合了文档中的两个示例,但“最高”并不是真正的赫兹(a 是 440 赫兹)。 你知道如何拥有比这更好的东西吗? import ddf.minim.*; import
我是一名优秀的程序员,十分优秀!