Get-Content command not recognized

2019-08-02 04:53发布

问题:

We have script which is normally executed within a unix environment. The following is a line from the script:

command => 'use/bin/tail -n 1 %{path} | grep --silent -F "%{message}" && rm -f {path}'

When run within PowerShell, the use/bin/tail path is obviously not recognized. As an alternative I tried this:

command => 'Get-Content -n 1 %{path} -Tail | grep --silent -F "%{message}" && rm -f {path}'

However, while Get-Content is recognized when run from the PowerShell command line itself, it is not recognized when run from within the script. This is the error message:

'Get-Content' is not recognized as an internal or external command, operable program or batch file.

What is the correct way to replicate the unix tail command when running a script in PowerShell?

回答1:

The error you receive:

%1 is not recognized as an internal or external command, operable program or batch file.

is a standard error from cmd.exe, not powershell.exe:

Your command is not getting executed by a powershell host, but by a regular command prompt - PowerShell would have mentioned the possibility of invoking a "cmdlet, function, script file or operable program" in case of "command not found" (misspelled Get-Content on purpose to demonstrate):

You'll also need to edit the command a little more, for PowerShell to make sense of it, the first statement in the pipeline would be:

Get-Content $Path -Tail 1

assuming that $Path contains the path to the file you're tailing.