0

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

我发现也导致了这一点。这基本上说我应该这样做:

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

代码在哪里SERVERNAME以及MATCH将给出...我的问题是我应该如何在 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。

4

1 回答 1

0

我没有尝试使用 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 调用的更多信息。

于 2013-07-01T03:04:10.517 回答