[英]How to use Powershell Invoke-Item with a gci search?
I'm trying to build a Powershell script that uses the Invoke-Item cmdlet once I've found the files using the following script:一旦我使用以下脚本找到文件,我正在尝试构建使用 Invoke-Item cmdlet 的 Powershell 脚本:
gci -Recurse -File *.xls* C:\folder_example\' | sort LastWriteTime | Select -last 2 |
I'm not sure how best to implement the Invoke-Item cmdlet from here?我不确定如何最好地从这里实现 Invoke-Item cmdlet?
In its simplest usage, Invoke-Item
(aliased as ii
) just needs a path given to it either through pipeline or through a parameter.在最简单的用法中, Invoke-Item
(别名为ii
)只需要通过管道或参数为其提供路径。
In this case, it could be as simple as adding | Invoke-Item
在这种情况下,它可以像添加| Invoke-Item
| Invoke-Item
to the end of your command: | Invoke-Item
到命令的末尾:
gci -Recurse -File *.xls* C:\folder_example\ | sort LastWriteTime | select -Last 2 | Invoke-Item
One thing I will mention, is that I have found that certain programs cannot handle a bunch of files thrown at them in quick succession like this script will do.我要提到的一件事是,我发现某些程序无法像这个脚本那样处理快速连续抛出的一堆文件。
I usually run into this when trying to open a bunch of files in Notepad++.我通常在尝试在 Notepad++ 中打开一堆文件时遇到这种情况。
I'm going to simplify the command a bit just to keep it short...but usually one of these two changes will fix my problem:我将稍微简化命令以使其简短......但通常这两个更改之一将解决我的问题:
Get-ChildItem -Filter *.xls* | ForEach-Object { Invoke-Item $_.FullName }
Placing the call into a ForEach-Object
script block will sometimes fix this.将调用放入ForEach-Object
脚本块有时会解决此问题。 I'm not 100% why this fixes it, I have some theories but that's all they are, so I will keep them out of this answer.我不是 100% 解决这个问题的原因,我有一些理论,但仅此而已,所以我会将它们排除在这个答案之外。 But it always fixes the issue for me when using Invoke-Item
on Notepad++ files.但是在 Notepad++ 文件上使用Invoke-Item
时,它总是为我解决了这个问题。
Some programs, like Excel, have a start up splash screen and some lag before they eventually open.一些程序,如 Excel,有一个启动闪屏,并且在最终打开之前会有一些延迟。 So you could always throw a sleep in there just to help slow things down a bit in case you are still having trouble:因此,如果您仍然遇到问题,您总是可以在那里睡觉以帮助放慢速度:
Get-ChildItem -Filter *.xls* | ForEach-Object { Start-Sleep 0.1; Invoke-Item $_.FullName }
That will add a small 100ms pause just to give whatever application you are running to have a little time to process.这将增加一个 100 毫秒的小暂停,以便让您正在运行的任何应用程序都有一点时间来处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.