Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Windows Server中从PowerShell执行Windows命令(获取内容)_Windows_Powershell - Fatal编程技术网

如何在Windows Server中从PowerShell执行Windows命令(获取内容)

如何在Windows Server中从PowerShell执行Windows命令(获取内容),windows,powershell,Windows,Powershell,我的服务器是Windows server。我想在Windows Server中复制Unixtail命令 Unix服务器:tail-f test.txt PowerShell:Get Content test.txt 如何在Windows Server中执行 以下命令不起作用: powershell -File "Get-Content test.txt" 错误消息: 无法执行程序“powershell-文件”\“获取内容 有什么想法吗?在powershell窗口中: Get-Content t

我的服务器是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-文件”\“获取内容


有什么想法吗?

在powershell窗口中:

Get-Content test.txt
命令返回:

你好,世界。 我在test.txt里面。 再见


获取内容
不是文件;这是一个cmdlet。Powershell.exe的
-file
参数指示Powershell读取提供的文件并作为脚本执行其中的命令

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

powershell -command "Get-Content test.txt"
在最简单的情况下

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

C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -command "Get-Content text.txt"
这个问题非常类似——也许本质上是相同的;我建议你阅读这个问题及其答案


此外,探索
获取内容的帮助将提供有用的信息。

在设置powershell.exe的完整路径且不带任何引号后工作正常


C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe-Command Get Content test.txt

该命令应失败,并出现其他错误。您到底是如何调用它的?
powershell-File“myScript.ps1”
vs
powershell-Command“Get Content test.txt”
@briantist也这样做的(并在文件后面添加
-Tail
),但他的错误消息表明出于某种原因,他的整个语句被解释为一个命令。@AnsgarWiechers同意;我想知道这是否是通过其他软件调用的这并没有回答OP的问题;他试图通过在外部调用Powershell来执行Get-Content cmdlet,但收到一个错误。谢谢Jeff Zeitlin。设置不带引号的powershell.exe的完整路径后工作正常
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe-命令Get Content test.txt