如何将文件的完整路径作为参数传递给Powershell的新示例?

lkaoscv7  于 2023-02-23  发布在  Shell
关注(0)|答案(1)|浏览(122)

下面是调用另一个脚本的脚本代码:

$LocalFolder = "$env:USERPROFILE\Desktop\Banana Productions"
$RenderClient = "$LocalFolder\Render\Modelo 02\Cliente 02"
$CutFolder = "$RenderClient\Cortar"
$FFMpegScript = "$CutFolder\ffmpeg crop.ps1" 

gci "$RenderClient\Cortar" *.mp4 -File -Recurse | ForEach-Object {
$FilePath = $_.FullName
start PowerShell "-NoExit", "-File", "`"$FFMpegScript`"", "$FilePath"
Write-Host $FilePath
}

问题是我无法将值为$_.FullName的参数传递给新示例,我在新示例中收到一条错误消息,消息如下:

Cannot process argument because the value of argument "name" is not valid

这就是我在剧本里说的:

param($args)

Write-Host $args[0]

Read-Host -Prompt "Press Enter to continue"

我该如何解决这个问题?

olhwl3o2

olhwl3o21#

由于Start-Process中的一个长期存在的bug-参见GitHub issue #5576-最好将一个 single 字符串参数传递给-ArgumentList参数(位置隐含),这允许您显式地控制进程命令行:
为了语法方便,下面的代码使用了可扩展的here-string:

Get-ChildItem "$RenderClient\Cortar" -Filter *.mp4 -File -Recurse |
  ForEach-Object {
    $FilePath = $_.FullName
    Start-Process PowerShell @"
-NoExit -File "$FFMpegScript" "$FilePath"
"@ # Note: This must be at the very start of the line.
  }

此外,不要将自动$args变量用作自定义变量。
事实上,如果你想让你的脚本只接收 * 位置 * 参数,那么就 * 不 * 需要正式的param(...)声明--只需要按原样使用数组值的$args变量:

# Without a param(...) declaration, $args *implicitly* contains
# any arguments passed, as an array.

$args[0] # output the 1st argument passed.

Read-Host -Prompt "Press Enter to continue"

相关问题