在 PowerShell 中处理拖到 BAT 文件的文件
Process files dragged to BAT file in PowerShell
我正在尝试创建一个脚本来转换放置在处理脚本上的 Markdown 文件。
为了实现这一点,我创建了一个 (process.bat
),它将放置的文件的名称传递给 PowerShell 脚本:
powershell.exe -NoProfile -File "./process.ps1" -Document %*
@pause
PowerShell 文件 (process.ps1
) 将单独处理每个文件:
[parameter(Mandatory=$true)]
[String[]]$Document
Write-Host $args[1]
$Document | ForEach-Object {
Write-Host "Document: $_"
# convert Markdown to Html
pandoc -o ($_ -Replace '.md', '.html') -f markdown -t html $_
}
当我将两个文件放在批处理文件上时:
C:\Users\XXX\Documents\WindowsPowerShell\Scripts\Markdown>powershell.exe -NoProfile -File "./process.ps1" -Document "C:\Users\XXX\Documents\WindowsPowerShell\Scripts\Markdown\FOO.md"
"C:\Users\XXX\Documents\WindowsPowerShell\Scripts\Markdown\BAR.md"
C:\Users\XXX\Documents\WindowsPowerShell\Scripts\Markdown\BAR.md
Document:
Press any key to continue . . .
文档尚未处理。
将批处理文件的文件列表 %*
传递给 PowerShell 的推荐方法是什么?
当使用 -File
参数从外部调用 powershell.exe
、PowerShell CLI 时 ,它 不会不支持 arrays - 仅 individual arguments.[1]
(此外,您忽略了将参数定义包装在 param(...)
块中,这实际上导致它被忽略。)
最简单的解决方案是 使用 ValueFromRemainingArguments
选项定义您的参数,以便它自动收集 所有 个位置参数 参数变量:
param(
[Parameter(Mandatory, ValueFromRemainingArguments)]
[String[]] $Document
)
然后在没有 -Document
的情况下通过 PowerShell CLI 调用您的脚本:
powershell.exe -NoProfile -File "./process.ps1" %*
作为使用 辅助批处理文件的替代方法,您可以:
定义一个快捷方式文件 (*.lnk
) 显式调用您的 PowerShell 脚本 powershell.exe -File \path\to\your\script.ps1
(没有附加参数)
然后使用那个作为放置目标.
注意:您不能使用 PowerShell 脚本 (*.ps1
) 直接 作为放置目标的原因是 PowerShell 脚本文件不是t 直接可执行 - 相反,打开(双击)*.ps1
文件 打开它进行编辑.
然后您需要将 pause
(或类似的东西,例如 Read-Host -Prompt 'Press Enter to exit.'
)添加到您的 PowerShell 脚本,以防止它在完成时立即关闭 window。
或者,保持脚本不变并使用 -NoExit
(放置在 之前 -File
)以保持 PowerShell 会话打开。
[1] PowerShell Core CLI, pwsh
.
同理
我正在尝试创建一个脚本来转换放置在处理脚本上的 Markdown 文件。
为了实现这一点,我创建了一个 (process.bat
),它将放置的文件的名称传递给 PowerShell 脚本:
powershell.exe -NoProfile -File "./process.ps1" -Document %*
@pause
PowerShell 文件 (process.ps1
) 将单独处理每个文件:
[parameter(Mandatory=$true)]
[String[]]$Document
Write-Host $args[1]
$Document | ForEach-Object {
Write-Host "Document: $_"
# convert Markdown to Html
pandoc -o ($_ -Replace '.md', '.html') -f markdown -t html $_
}
当我将两个文件放在批处理文件上时:
C:\Users\XXX\Documents\WindowsPowerShell\Scripts\Markdown>powershell.exe -NoProfile -File "./process.ps1" -Document "C:\Users\XXX\Documents\WindowsPowerShell\Scripts\Markdown\FOO.md"
"C:\Users\XXX\Documents\WindowsPowerShell\Scripts\Markdown\BAR.md"
C:\Users\XXX\Documents\WindowsPowerShell\Scripts\Markdown\BAR.md
Document:
Press any key to continue . . .
文档尚未处理。
将批处理文件的文件列表 %*
传递给 PowerShell 的推荐方法是什么?
当使用 -File
参数从外部调用 powershell.exe
、PowerShell CLI 时 ,它 不会不支持 arrays - 仅 individual arguments.[1]
(此外,您忽略了将参数定义包装在 param(...)
块中,这实际上导致它被忽略。)
最简单的解决方案是 使用 ValueFromRemainingArguments
选项定义您的参数,以便它自动收集 所有 个位置参数 参数变量:
param(
[Parameter(Mandatory, ValueFromRemainingArguments)]
[String[]] $Document
)
然后在没有 -Document
的情况下通过 PowerShell CLI 调用您的脚本:
powershell.exe -NoProfile -File "./process.ps1" %*
作为使用 辅助批处理文件的替代方法,您可以:
定义一个快捷方式文件 (
*.lnk
) 显式调用您的 PowerShell 脚本powershell.exe -File \path\to\your\script.ps1
(没有附加参数)然后使用那个作为放置目标.
注意:您不能使用 PowerShell 脚本 (*.ps1
) 直接 作为放置目标的原因是 PowerShell 脚本文件不是t 直接可执行 - 相反,打开(双击)*.ps1
文件 打开它进行编辑.
然后您需要将 pause
(或类似的东西,例如 Read-Host -Prompt 'Press Enter to exit.'
)添加到您的 PowerShell 脚本,以防止它在完成时立即关闭 window。
或者,保持脚本不变并使用 -NoExit
(放置在 之前 -File
)以保持 PowerShell 会话打开。
[1] PowerShell Core CLI, pwsh
.