Pipeline input to executable with PowerShell

1

I need to execute the following command in PowerShell:

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

I am trying to do it like this:

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

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

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

& $appCmd $AllArgs | Get-Content $targetFilePath

But this is apperantly wrong since it gives me an error:

The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

Please assist on what is the correct alternative to the above mentioned script in PowerShell.

Maxim V. Pavlov

Posted 2013-10-04T22:44:13.940

Reputation: 1 432

Answers

4

PowerShell pipe takes input on the left, and passes it into the command on the right. In this case, you are passing the output of your command to Get-Content, which doesn't take an input parameter.

Change your call line so that the input flows from left to right:

Get-Content $targetFilePath | & $appCmd $AllArgs

See this answer on StackOverflow for an example.

Frank Thomas

Posted 2013-10-04T22:44:13.940

Reputation: 29 039