【问题标题】:Powershell to connect to an exchange server and remove messages from the QueuePowershell 连接到交换服务器并从队列中删除消息
【发布时间】:2013-05-24 08:39:09
【问题描述】:

我有 Python 代码,它应该使用 Powershell 连接到交换服务器并从队列中删除消息。 (我是 Powershell 新手)

我找到了this,这也导致了this。这基本上说我应该这样做:

Remove-Message -Server SERVERNAME -Filter {FromAddress -like '*MATCH*'} -WithNDR $false

代码将给出SERVERNAMEMATCH 的位置...我的问题是我应该如何在Powershell 中运行它?所以我这样做了:

powershell -NoExit -Command "COMMAND"

COMMAND 是上面的命令。我想用子进程运行它。当我手动测试命令时,出现此错误:

The term 'Remove-Message' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:15
+ Remove-Message <<<< -Server SERVERNAME -Filter {FromAddress -like '*MATCH*'} -WithNDR $false
    + CategoryInfo : ObjectNotFound: (Remove-Message:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

所以我认为我做错了什么,尽管我认为我将参数正确地传递到了 Powershell。

【问题讨论】:

    标签: python powershell exchange-server exchange-server-2010


    【解决方案1】:

    我没有尝试使用 remove-message cmdlet。假设您提供的命令可以按预期工作。你应该做以下事情,

    1) 编写一个脚本来包含命令,包括连接到交换和删除消息

        if ((gcm Remove-Message -ErrorAction Ignore) -eq $null)
        {
          $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://$($env:COMPUTERNAME).$($env:USERDNSDOMAIN)/Powershell?serializationLevel=Full;ExchClientVer=14.2.247.5" -Authentication kerberos
          Import-Module (Import-PSSession $session) -global
        }
        Remove-Message -Server SERVERNAME -Filter {FromAddress -like '*MATCH*'} -WithNDR $false
    

    请对-ConnectionUri进行适当的更改,因为我只是展示了如何使用完全限定的域名连接到本地。

    2) 使用以下代码调用此脚本,请勿使用 -NoExit,除非您不想在删除完成后继续使用该 powershell 控制台。

    powershell -command ". 'PS_FILE_PATH'"
    

    3) 如果您需要将参数传递到脚本中,您可以将它们组合成-command ". 'script.ps1' arg1 arg2"。并在脚本中使用$args[0],或者先使用param($match,$servername) 声明参数,然后在脚本中使用。

    我对 Python 不是很熟悉,所以无法提供有关如何使用 python 调用的更多信息。

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 2020-01-10
      • 2012-09-19
      • 2012-06-05
      • 1970-01-01
      • 2023-04-06
      • 2022-01-13
      • 2017-01-16
      相关资源
      最近更新 更多