我正在cmd中执行powershell脚本。
首先我写命令
C:\Windows\system32>start powershell.exe Set-ExecutionPolicy RemoteSigned
运行成功
比运行脚本我写命令
C:\Windows\system32>start powershell.exe C:\\Get-NetworkStatistics.ps1
也成功运行了
问题是当我尝试运行该函数时
C:\Windows\system32>start powershell.exe Get-NetworkStatistics -computername Gbsi1 | Format-Table -autosize
它给出错误“‘Format-Table’未被识别为内部或外部命令, 可运行的程序或批处理文件。”
这是它的屏幕截图。
在powershell中运行成功,但在cmd中运行失败。管道有问题吗|我把它放在格式表之前
正如您所言,管道由
CMD
解释,而不是 powershell。
因此,CMD
将尝试执行名为Format-Table
的命令,该命令不存在(在powershell之外)。
您可以使用
^
: 来逃避它
start powershell.exe Get-NetworkStatistics -computername Gbsi1 ^| Format-Table -autosize
或者通过引用完整的命令行
start powershell.exe "Get-NetworkStatistics -computername Gbsi1 | Format-Table -autosize"
请注意,无论如何,您的调用都是错误的,您需要向 powershell 提供
-Command
选项,如下所示:
start powershell.exe -Command "Get-NetworkStatistics -computername Gbsi1 | Format-Table -autosize"
最后,你真的想用
start
吗?它将打开一个新窗口,该窗口将在命令完成后立即关闭。您还可以使用:
powershell.exe -Command "Get-NetworkStatistics -computername Gbsi1 | Format-Table -autosize"
谢谢它适用于 Windows 10。 因为我在运行 Nodemon 命令时遇到安全错误。