使用管道运行单个powershell命令

使用管道运行单个powershell命令,powershell,Powershell,我想跑 powershell.exe -command "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; Get-MailboxExportRequest -Status Completed | Remove-MailboxExportRequest" 但是由于某种原因,命令挂起,可能是因为管道的解释不同。有没有办法将其作为管道而不是逻辑OR来运行?命令参数使用scriptblock,请在如下字符串中定义它: powe

我想跑

powershell.exe -command "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; Get-MailboxExportRequest -Status Completed | Remove-MailboxExportRequest"
但是由于某种原因,命令挂起,可能是因为管道的解释不同。有没有办法将其作为管道而不是逻辑OR来运行?

命令参数使用scriptblock,请在如下字符串中定义它:

powershell.exe -command "& {Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; Get-MailboxExportRequest -Status Completed | Remove-MailboxExportRequest}"
或者首先使用脚本块:

powershell.exe -command {Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; Get-MailboxExportRequest -Status Completed | Remove-MailboxExportRequest }

不过,最好的方法是将其放入脚本文件并使用-file参数。如果您将此用于计划任务,我建议您使用后者。

这不是真的。From:执行指定的命令和任何参数,就像它们是在Windows PowerShell命令提示符下键入的一样,然后退出,除非指定了NoExit参数。。您应该能够在其中放置一个脚本块。在PowerShell会话中尝试这样做会发生什么?将假定问题与Add-PSSnapin有关