【问题标题】:Get PID from open Excel file从打开的 Excel 文件中获取 PID
【发布时间】:2021-10-23 22:38:24
【问题描述】:

有没有办法获取已打开特定文件的 Excel 进程的 pid 并将该 pid 设置在诸如“FilePID”之类的变量中?

有点像:

Get-Process -name EXCEL file:"C:\Excel\file.xlsx" -> $FilePID

【问题讨论】:

  • $FilePID = (Get-Process -name Excel | Where-Object { $_.MainWindowTitle -like 'yourfilename.xlsx*' }).Id

标签: excel powershell


【解决方案1】:

如果您知道在 Excel 中打开的文件的名称,您可以这样做:

$FilePID = (Get-Process -name Excel | Where-Object { $_.MainWindowTitle -like 'yourfilename.xlsx*' }).Id

通常,Excel 在其标题栏中显示类似 yourfilename.xlsx - Excel 的内容,这就是尾随通配符 * 的原因

【讨论】:

  • 这很完美!!非常感谢西奥!但它只适用于“$Excel.visible = $true”。如果我使用“$false”,它就不起作用。有解决办法吗?
  • @ASD 为此我猜你需要编写一些 C# 代码来使用来自 user32.dll 的 GetWindowText 还没有解决这个问题..
  • 你知道吗...我想我尝试过的大多数其他事情都因为“$false”而不起作用。难以置信......
  • @ASD 是的。我只是在$Excel.visible = $false 时使用 Excel 打开文件进行了一些调查,结果发现在这种情况下 Excel 不会填充 .MainWindowTitle 甚至设置 @987654327 @ to 0,所以我认为不可能..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-30
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
相关资源
最近更新 更多