我正在 cmd 中执行 powershell 脚本。
首先我写命令
C:\Windows\system32>启动 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 中运行失败。管道有问题吗?我把它放在 Format-Table 之前
最佳答案
正如您所说,管道由 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 - PowerShell 在命令提示符问题中, 'Format-Table' 未被识别为内部或外部命令、可运行程序或批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24159026/