使用 PowerShell 將輸入管道輸入到執行檔

使用 PowerShell 將輸入管道輸入到執行檔

我需要在 PowerShell 中執行以下命令:

%windir%\system32\inetsrv\appcmd add site /in < c:\mywebsite.xml

我正在嘗試這樣做:

$appCmd = "$Env:SystemRoot\system32\inetsrv\appcmd.exe"      

[String] $targetFilePath = $restoreFromDirectory + "config.xml"

$AllArgs = @('add', 'site', '/in')

& $appCmd $AllArgs | Get-Content $targetFilePath

但這顯然是錯的,因為它給了我一個錯誤:

輸入物件無法綁定到命令的任何參數,因為此命令不採用管道輸入,或輸入及其屬性與採用管道輸入的任何參數都不匹配。

請協助了解 PowerShell 中上述腳本的正確替代方案是什麼。

答案1

PowerShell 管道接受左側的輸入,並將其傳遞到右側的命令中。在這種情況下,您將命令的輸出傳遞給Get-Content,它不接受輸入參數。

更改您的呼叫線路,以便輸入從左到右流動:

Get-Content $targetFilePath | & $appCmd $AllArgs

看到這個StackOverflow 上的回答舉個例子。

相關內容