我有一個 cmd 檔案,它呼叫一個顯示輸入的 powershell 腳本。
cmd 的輸入是一個檔案名串列,它將其轉發給接受字串陣列的 powershellscript。
嘗試時,整個檔案名串列作為一個引數。
我在這里和這里的鏈接嘗試了答案,但沒有運氣。
下面是我運行 cmd 時的輸出。
C:\Users\User1>C:\Sample.cmd "C:\file1.txt C:\file2.txt"
Processing file - C:\file1.txt C:\file2.txt
不幸的是,cmd(檔案串列)的輸入是從呼叫它的外部程式接收的。
powershell 腳本如下所示:
param
(
[Parameter(Position = 0, Mandatory = $true)]
[string[]] $sourceFiles
)
Function Sample_function
{
Param
(
[Parameter(Position = 0, Mandatory = $true)]
[string[]] $sourceFiles
)
foreach($file in $sourceFiles)
{
Write-Host "Processing file - $file"
}
}
Sample_function $sourceFiles
cmd是這樣的:
@echo off
set PS_File="C:\Sample.ps1"
powershell -FILE "%PS_File%" %*
uj5u.com熱心網友回復:
為了使陣列引數與 一起%*使用,請使用以下ValueFromRemainingArguments設定:
param
(
[Parameter(Position = 0, Mandatory = $true, ValueFromRemainingArguments = $true)]
[string[]] $sourceFiles
)
現在 PowerShell 將正確系結所有擴展的引數值,$sourceFiles即使它們由空格分隔,而不是,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444139.html
