如何在Windows Server中从PowerShell执行Windows命令(Get-Content)



我的服务器是Windows Server。我想在Windows Server中复制Unix tail命令。

Unix 服务器:tail -f test.txt

PowerShell: Get-Content test.txt

如何在Windows Server中执行?

以下命令不起作用:

powershell -File "Get-Content test.txt"

错误信息:

无法执行程序"powershell -文件 "\"获取内容...

知道吗?

Get-Content不是

文件;它是一个cmdlet。Powershell 的 -file 参数.exe指示 Powershell 读取提供的文件,并以脚本形式执行其中的命令。

可以使用 -command 参数将命令直接传递给 Powershell;该参数可以是带引号的字符串,该字符串被解释为 Powershell 命令。因此,您可能希望使用

powershell -command "Get-Content test.txt"

在最简单的情况下。

请注意,Powershell.exe 必须在您的系统路径中;如果不是,则需要提供 powershell 的完整路径,例如:

C:WINDOWSSystem32WindowsPowerShellv1.0powershell.exe -command "Get-Content text.txt"

这个问题与Windows Powershell中的Unix尾等效命令非常相似 - 也许本质上相同;我建议阅读这个问题及其答案。

此外,探索Get-Content帮助将提供有用的信息。

设置powershell的完整路径后工作正常.exe并且没有任何引号

C:WindowsSystem32WindowsPowerShellv1.0powershell.exe -Command Get-Content test.txt

在powershell窗口中:

Get-Content test.txt

命令返回:

世界您好。 我在测试.txt里面。 再见。

最新更新