简体   繁体   English

如何从批处理文件自动运行交换 powershell 命令

[英]How to run exchange powershell command automatically from batch file

I have a .bat file with the following code:我有一个包含以下代码的.bat文件:

START C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2.0 -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto"

The code above will successfully start a new powershell host and then connect to my exchange server.上面的代码将成功启动一个新的 powershell 主机,然后连接到我的交换服务器。

How can I run the below command in the exchange powershell host, from the same batch file?如何从同一个batch文件在交换 powershell 主机中运行以下命令? (basically I am trying to automate the process) (基本上我正在尝试自动化该过程)

Get-MessageTrackingLog -resultsize unlimited -start "01/01/2020 00:00:00" -Server hermod -EventId Deliver | where {[string]$_.sender -like '*@gmail.com'} | where {[string]$_.Recipients -like '*@gmail.com'} > "C:\MailReporter\Output\emails.txt"

I worked it out: I needed to use the ;我解决了:我需要使用; symbol after the first command to signal the end of the current command.第一个命令后的符号表示当前命令的结束。 I was then able to execute the next command without issue.然后我就可以毫无问题地执行下一个命令。

Full working code:完整的工作代码:

START C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2.0 -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Get-MessageTrackingLog -resultsize unlimited -start '01/09/2019 00:00:00' -Server hermod -EventId Deliver | where {[string]$_.sender -like '*@gmail.co.uk'} | where {[string]$_.Recipients -like '*@gmail.co.uk'} > 'C:\MailReporter\Output\emails.txt' "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM