问题描述
是否可以将if/else捕获添加到仅
$ excelId =获取进程excel |%{$ _.Id} |?{$ before -notcontains $ _}
是否没有运行excel进程?例如如果 Excel 正在运行,则获取进程ID,否则请忽略它.
我的代码的3-5行如下:
$ before = @(get-process excel |%{$ _.Id})$ excel =新对象-com excel.application$ excelId =获取流程excel |%{$ _.Id} |?{$ before -notcontains $ _}
我会这样做:
$ before = Get-Process |%{$ _.Id}$ excel =新对象-com excel.application$ excelId =获取流程excel |%{$ _.Id} |?{$ before -notcontains $ _}
通过仅预先收集所有进程的ID来回避此问题.这样,无论是否存在现有的Excel流程,我们都将遵循相同的步骤.如果没有,则过滤器?{$ before -notcontains $ _}
将不匹配并排除任何进程,我们将取回新进程的PID.
但是,我不同意 Efran Cobisi的回答:这是处理这种特殊情况的另一种有效方法./p>
Is there way to add an if/else catch to only
$excelId = get-process excel | %{$_.Id} | ?{$before -notcontains $_}
if there is no excel process running? e.g. if Excel is running then get-process id, if not then ignore it.
Line 3-5 of my code is as follows:
$before = @(get-process excel | %{$_.Id} )
$excel=new-object -com excel.application
$excelId = get-process excel | %{$_.Id} | ?{$before -notcontains $_}
I would do it like so:
$before = Get-Process | % { $_.Id }
$excel=new-object -com excel.application
$excelId = Get-Process excel | % { $_.Id } | ? { $before -notcontains $_ }
This sidesteps the issue by just collecting IDs for all processes beforehand. This way, we follow the same steps whether or not there are existing Excel processes. If there are none, then the filter ? { $before -notcontains $_ }
will not match and exclude any processes, and we'll just get back the PID for our new process.
However, I don't disagree with Efran Cobisi's answer: it's another valid way to handle this particular scenario.
这篇关于Powershell-添加捕获以在没有Excel流程的情况下进行提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!