我试图非常简单地运行一个可执行文件并传递一个文件path作为第一个参数。 在DOS中,这将是命令:
import.exe "C:\Some Path\With\Spaces.txt"
通过将path放在引号中,path将作为第一个标记正确parsing到可执行文件中。
在PowerShell中,我使用这个:
$feeds = dir 'T:\Documents\Company\Product Development\Data foreach ($feed in $feeds) { start -FilePath import.exe -ArgumentList $feed.FullName }
PowerShell中的Start-Process cmdlet的问题在于它使用string数组作为参数,所以path被分解并作为单独的标记发送到可执行文件,或者作为没有这些重要的引号的string有效地发送。
将引号添加到我的PowerShell命令行强制“$ feed.FullName”从字面上处理。
而双引号使得PowerShell在参数列表中看不到任何东西。 “论点是空的或空的。” 它告诉我。
我期望这是一个已知的头痛,从一开始就有一个已知的解决方法。
谢谢
卢克
更新和响应
foreach ($feed in $feeds) { Invoke-Expression "start import.exe `"$feed.FullName`"" }
注意:我使用的是Start,因为尽pipe将我的位置设置到了import.exe所在的文件夹,PowerShell似乎已经被写入来查找文件系统当前位置的可执行文件。
该variables被解释与.FullName作为一个文字string!:
filename.txt.FullName
这个:
foreach ($feed in $feeds) { Invoke-Expression "C:\Users\Public\Documents\~Main\Data.Importer\Data.Importer\bin\Release\import.exe ($feed.FullName)" }
结果是:
Invoke-Expression : Unexpected token '_data.txt.FullName' in expression or statement.
有趣的是,它自己可以工作:
C:\Users\Public\Documents\~Main\Data.Importer\Data.Importer\bin\Release\import.exe $feed.FullName
但是这个:
foreach ($feed in $feeds) { Invoke-Expression C:\Users\Public\Documents\~Main\Vuplan\Evoq.Vuplan.Data.Epg.Importer\Evoq.Vuplan.Data.Epg.Importer\bin\Release\import.exe $feed.FullName }
结果在这:
Invoke-Expression : A positional parameter cannot be found that accepts argument 'T:\Documents\Company\Product Development\Data\3112_data.txt'.
怎么样:
Invoke-Expression“start import.exe'$($ feed.FullName)'”
import.exe'“C:\ Some Path \ With \ Spaces.txt”'为我做:)在单引号内embedded双引号。
这种事情可以用invoke-expression cmdlet解决
invoke-expression "import.exe `"C:\Some Path\With\Spaces.txt`""
反引号将被解释并且应该用指定的string开始import.exe。
在Powershell中直接调用DOS命令。
例如:
cmd.exe /c start import.exe $myParameter