在ForEach循环中运行程序 [英] Run a program in a ForEach loop

查看:106
本文介绍了在ForEach循环中运行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使此简单的PowerShell脚本正常工作,但我认为某些根本上是错误的. ;-)

I'm trying to get this simple PowerShell script working, but I think something is fundamentally wrong. ;-)

ls | ForEach { "C:\Working\tools\custom-tool.exe" $_ }

我基本上想获取目录中的文件,并将它们作为自定义工具的参数逐一传递.

I basically want to get files in a directory, and pass them one by one as arguments to the custom tool.

推荐答案

ls | %{C:\Working\tools\custom-tool.exe $_}

当每个对象沿管线下降时,将针对该对象运行该工具.在命令字符串两边加上引号会使它成为...一个字符串!这样,本地变量"$ _"很可能不知道如何处理,因此会吐出错误.

As each object comes down the pipeline the tool will be run against it. Putting quotes around the command string causes it to be... a string! The local variable "$_" it then likely doesn't know what to do with so pukes with an error.

这篇关于在ForEach循环中运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆