[英]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.