我正在执行cmd中的powershell脚本。
首先我写命令
C:\Windows\system32>start powershell.exe Set-ExecutionPolicy RemoteSigned
它工作成功
比运行脚本我写命令
C:\Windows\system32>start powershell.exe C:\\Get-NetworkStatistics.ps1
它也工作成功
问题是当我尝试运行该function
C:\Windows\system32>start powershell.exe Get-NetworkStatistics -computername Gbsi1 | Format-Table -autosize
它会给出“Format-Table”不被识别为内部或外部命令,可操作程序或batch file的错误。
这是它的截图。
它在PowerShell中成功运行,但没有在CMD中。 pipe道是否有问题? 我把之前格式表
就像你这样,管道是由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"
请注意,无论如何,您的调用是错误的,您需要为-Command
提供-Command
选项,如下所示:
start powershell.exe -Command "Get-NetworkStatistics -computername Gbsi1 | Format-Table -autosize"
最后,你真的想用start
? 它会打开一个新窗口,在命令通过后立即关闭。 你也可以使用:
powershell.exe -Command "Get-NetworkStatistics -computername Gbsi1 | Format-Table -autosize"