将参数传递给 Powershell 中的另一个程序
我正在尝试编写一个脚本来解析目录,在具有相同名称但扩展名不同的其他文件中找到给定扩展名的 3 个最新文件,然后将输出传递给命令行程序。
到目前为止,我已经:
$file = dir test -Exclude *.md5 | sort -prop LastWriteTime | select fullname -last 3
\path\program argumentA $file[0] argumentB "argumentC"
\path\program argumentA $file[1] argumentB "argumentC"
\path\program argumentA $file[2] argumentB "argumentC"
不确定它是否很重要,但 argument2 需要用引号引起来,程序才能使用它。我最终要么将输出写入文件,要么最好在奖励问题中描述的另一行代码中使用输出。
我遇到的问题是,当我使用 $file[0] 作为参数时,什么也没有发生。当我使用存储在 $file[0] 中的文件名而不是使用变量时,它会运行,但不会传递 argumentB 或 argumentC。如果我从 cmd 运行它,我会得到正确的输出。
我的问题是: 如何让程序识别变量? 如何让程序识别其他参数?
奖金问题: 该程序将输出文件列表。如何删除给定文件夹中除程序输出中列出的文件之外的所有内容?
I am trying to write a script that will parse a directory, find the 3 newest files of a given extension among other files with the same name but different extension and then pass the output to a command line program.
So far I have:
$file = dir test -Exclude *.md5 | sort -prop LastWriteTime | select fullname -last 3
\path\program argumentA $file[0] argumentB "argumentC"
\path\program argumentA $file[1] argumentB "argumentC"
\path\program argumentA $file[2] argumentB "argumentC"
Not sure if it matters a whole lot but argument2 needs to be in quotes for the program to use it. I will eventually either have the output written to a file or, preferably use the output in another line of code described in the bonus question.
The problem I am having is that when I use $file[0] as an argument nothing happens. When I use the file name stored in $file[0] instead of using the variable, it runs, but does not pass argumentB or argumentC. If I run it from cmd I get the proper output.
My questions are:
How do I get the program to recognize a variable?
How do I get the program to recognize the other arguments?
Bonus question:
The program will output a list of files. How do I delete everything in the given folder except for the files listed in the output of the program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下
,我发现我经常需要将一些东西放在“神奇的”美元符号括号中才能让它们在 PowerShell 中工作。
Try
I find that I often have to wrap things in "magical" dollar sign brackets to get them working in PowerShell.
由于您正在通过管道传输到
Select-Object
并选择FullName
属性,因此您有 3 个System.Management.Automation.PSCustomObject
类型的对象,其中带有 < code>FullName 属性。将对象用作程序的命令行参数时,您应该访问对象的FullName
属性,如下所示:如果从
Select-Object
cmdlet Powershell 中删除 fullname 属性不会创建 PSCustomObject,您可以访问原始 System.IO.FileInfo 对象的所有属性和方法。您可以通过以下方式查看差异:
PsCustomObject
System.IO.FileInfo
Since you are piping to
Select-Object
and selecting theFullName
property you have 3 objects of typeSystem.Management.Automation.PSCustomObject
with aFullName
property. You should access theFullName
property on the object when using it as a command line argument for your program like this:If you remove the fullname property from the
Select-Object
cmdlet Powershell will not create a PSCustomObject and you can access all properties and methods from the originalSystem.IO.FileInfo
object.This is how you can see the difference:
PsCustomObject
System.IO.FileInfo
我实际上会使用一个循环来避免重复。 (这样您就可以避免复制粘贴错误并简化问题,特别是如果您稍后添加错误处理或其他任何内容)。
由于 PS 无论如何都将集合视为与普通对象相同,因此即使您只选择 1 个对象,此操作也有效。
I would actually use a loop with this to avoid duplication. (That way you'll avoid copy-and-paste errors and simplify the problem, especially if you add error-handling or anything else later).
As PS treats collections the same as normal objects anyway, this works even if you only select 1 object.