无法识别Get-Content命令 [英] Get-Content command not recognized

查看:211
本文介绍了无法识别Get-Content命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有通常在unix环境中执行的脚本. 以下是脚本中的一行:

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}'

在PowerShell中运行时,显然无法识别use/bin/tail路径.作为替代方案,我尝试了以下方法:

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}'

但是,虽然从PowerShell命令行本身运行时可以识别Get-Content,但是从脚本内部运行时却不能识别. 这是错误消息:

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.

在PowerShell中运行脚本时,复制unix tail命令的正确方法是什么?

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

推荐答案

您收到的错误:

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

是来自cmd.exe而不是powershell.exe的标准错误:

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

您的命令不是由Powershell主机执行的,而是由常规命令提示符执行的-PowerShell将提到在找不到命令"的情况下调用"cmdlet,函数,脚本文件或可运行程序"的可能性(故意拼写错误的Get-Content来演示):

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):

您还需要多一点编辑命令,为了使PowerShell能够理解它,管道中的第一条语句将是:

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

假定$Path包含要尾随的文件的路径.

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

这篇关于无法识别Get-Content命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆