通过命令提示符调用PowerShell不允许使用管道"|"。 作为一个简单的示例,以下不选择"全名"
C:>powershell -ExecutionPolicy RemoteSigned -noprofile -noninterac
tive Invoke-Command -Computer Remote.Computer -ScriptBlock { Import-Module
'C:ScriptsRunVirtualMachineManager.ps1'; ls " | select FullName" }
我无法测试这个,但这看起来更好:
powershell -ExecutionPolicy RemoteSigned -noprofile -noninteractive "Invoke-Command -Computer Remote.Computer -ScriptBlock { Import-Module 'C:ScriptsRunVirtualMachineManager.ps1'; ls | select FullName }"
您尝试导入的模块不应该以 .psm1 结尾吗?
例如,这对我有用:
powershell -Executionpolicy RemoteSigned -noprofile -noninteractive "Get-Process | Select ProcessName"
要运行的命令(包括管道)都放在引号内。
以下清理版本对我们有用(使用"选择计数"示例):C:>powershell -ExecutionPolicy RemoteSigned -noprofile -noninteractive "&{Invoke-Command -Computer Remote.Computer -ScriptBlock { import-Module 'C:\\.ps1' |出空; |选择计数 |格式列表 }}"