我以为我对此有了答案,但是我玩的越多,我就越认为它是Powershell的设计缺陷.
我想拖放(或使用Send-To机制)将多个文件和/或文件夹作为数组传递给Powershell脚本.
测试脚本
#Test.ps1
param ( [string[]] $Paths, [string] $ExampleParameter )
"Paths"
$Paths
"args"
$args
Run Code Online (Sandbox Code Playgroud)
尝试#1
我使用以下命令行创建了一个快捷方式,并将一些文件拖到它上面.这些文件作为单独的参数出现,这些参数首先匹配脚本参数的位置,其余部分放在$ args数组中.
尝试#1的快捷方式
powershell.exe -noprofile -noexit -file c:\Test.ps1
Run Code Online (Sandbox Code Playgroud)
包装脚本
我发现我可以用包装脚本做到这一点......
#TestWrapper.ps1
& .\Test.ps1 -Paths $args
Run Code Online (Sandbox Code Playgroud)
包装脚本的快捷方式
powershell.exe -noprofile -noexit -file c:\TestWrapper.ps1
Run Code Online (Sandbox Code Playgroud)
批处理文件包装器脚本
它通过批处理文件包装脚本工作...
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%}"
Run Code Online (Sandbox Code Playgroud)
尝试回答
Keith Hill提出了使用以下快捷命令行的优秀建议,但它没有正确传递参数.当到达Test.ps1脚本时,带有空格的路径被拆分.
powershell.exe -noprofile -noexit -command "& {c:\test1.ps1 $args}"
Run Code Online (Sandbox Code Playgroud)
有没有人找到一种方法来做到这一点没有额外的脚本?
由于某种原因,我根本无法理解大多数解释此问题的网站。因此,我将尝试在这里提问,如果我在错误的位置,请在评论中告诉我,然后将其放在另一个论坛中并删除此问题。
假设我有2个文件,Batch.bat和PowerShell.ps1。
Batch.bat:
set A="ThisIsSuchVar!"
Run Code Online (Sandbox Code Playgroud)
PowerShell.ps1:
$B = "Well, i don't know what to do here"
Run Code Online (Sandbox Code Playgroud)
我该如何做与B变量相同的A变量?
记住:我希望Batch变量进入PowerShell文件。这是一种单向脚本。我想使用内置的Windows源。而且,请考虑一下我是编程方面的新手,而且英语说得不太好,所以请尽可能简单一些。