我认为我有一个答案,但我越玩它,我就越认为这是PowerShell的一个设计缺陷。拖放到PowerShell脚本
我想拖放(或使用发送到机构)来传递多个文件和/或文件夹作为一个阵列到Powershell的脚本。
测试脚本
#Test.ps1
param ([string[]] $Paths, [string] $ExampleParameter)
"Paths"
$Paths
"args"
$args
尝试#1
我创建了下面的命令行的快捷方式,并拖曳到它的一些文件。这些文件会作为单独的参数出现,首先在位置上与脚本参数匹配,其余部分放置在$ args数组中。
快捷方式尝试#1
powershell.exe -noprofile -noexit -file c:\Test.ps1
包装脚本
我发现我可以用一个包装脚本做到这一点...
#TestWrapper.ps1
& .\Test.ps1 -Paths $args
快捷方式包装脚本
powershell.exe -noprofile -noexit -file c:\TestWrapper.ps1
批处理文件包装脚本
和它的作品通过批处理文件包装脚本...
REM TestWrapper.bat
SET args='%1'
:More
SHIFT
IF '%1' == '' GOTO Done
SET args=%args%,'%1'
GOTO More
:Done
Powershell.exe -noprofile -noexit -command "& {c:\test.ps1 %args%}"
试图解答
基思·希尔的出色suggestion使用下面的快捷方式命令行,但它没有传递参数c orrectly。带有空格的路径在到达Test.ps1脚本时被拆分。
powershell.exe -noprofile -noexit -command "& {c:\test1.ps1 $args}"
有没有人找到一种方法来做到这一点,没有额外的脚本?
下面是一个替代的解决方案,尽管它会[需要适应的powershell](HTTP://mathematica.stackexchange。 com/a/78414/5615) – William 2015-03-28 01:18:43
用于记录...如果脚本使用Powershell -File ScriptName.ps1运行,并且该脚本没有param块,则$ args变量将精确包含what创建一个文件路径数组。 – 2016-01-13 22:25:28
这里是一个[bug报告](https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/20575477--bug-drag-and-drop-to-scripts-shortcuts-split-pat)你可以upvote在Powershell的UserVoice上。 – 2017-08-22 14:31:07