繁体   English   中英

如何在 Powershell 的 Start-Process 中将数组传递给 arguments?

[英]How to pass an array to the arguments in Start-Process in Powershell?

我正在编写一个脚本来播放播放器中的某些文件。 我使用 Get-ChildItem 来获取文件名数组。 然后我想用 Start-Process 来播放这些文件。 但是,如何将这些文件名添加到播放器程序的 arguments 中?

我使用Start-Process -FilePath "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" -ArgumentList $selected_items但它似乎不起作用并且文件没有播放。

注意文件名中有空格。

可用的命令行选项来看,以下可能有效(我无法亲自验证):

/clipboard :将剪贴板中的内容附加到播放列表中并立即开始播放。

# Copy the full names of the files of interest to the clipboard.
Set-Clipboard -Value $selected_items.FullName

# Launch the player and tell it to start playback of the files on the clipboard.
# Parameters -FilePath and -ArgumentList are positionally implied.
Start-Process 'C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe' /clipboard

有基于文件参数的选项,例如/new/insert/add ,但我不清楚它们是否会自动开始播放(可能取决于应用程序的配置)。

您可以ForEach-Object

Get-ChildItem . | ForEach-Object {Start-Process -FilePath "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" -ArgumentList $_.FullName}

您不需要启动过程。

& "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" 
C:\Program` Files\DAUM\PotPlayer\PotPlayerMini64.exe
$env:path += ';C:\Program Files\DAUM\PotPlayer'; PotPlayerMini64

我自己无法对此进行测试,但看起来 switch /content可以采用由空格字符连接的文件路径数组。

$selected_items = Get-ChildItem -Path 'X:\pathtothefiles' -File | ForEach-Object {
    # quote the file paths
    '"{0}"' -f $_.FullName
}

$player = "C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe"
Start-Process -FilePath $player /content ($selected_items -join ' ')

# or
# & $player /content ($selected_items -join ' ')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM