powershell - 无法在 Powershell 中使用带有管道变量的 -filter 子句运行 Get-CASMailbox

标签 powershell exchange-server exchange-server-2007

我在使用 Exchange cmdlet Get-CASMailbox 的语法时遇到了一些问题。

我在 2007 环境中运行,并且在 PowerGUI 2.4、3.0 和 PS ISE 2.0 中进行了测试。

用于演示目的的示例代码:

[String[]] $MailServerList = @('IP-0A207B07')

$MailServerList | %{            
Get-CASMailbox -ResultSize:10 -Filter {ServerName -eq $_}   
}

在此示例中,只有一个邮件服务器。在调用 Get-CASMailbox 期间,管道运算符 $_ 是 IP-0A207B07 并且 $_.GetType() 确认它是一个字符串。如果此行运行,我会收到以下错误:

Get-CASMailbox:无法将参数“过滤器”绑定(bind)到目标。异常设置“过滤器”:“无法转换'System.Management.Automa'类型的对象
tion.PSObject' 来输入 'System.String'。”
在 C:\Users\erawlins\Desktop\MailboxFilterBug.ps1:7 char:38
+ Get-CASMailbox -ResultSize:10 -Filter <<<< {ServerName -eq $_}
+ CategoryInfo : WriteError: (:) [Get-CASMailbox], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.GetCASMailbox

我真的很困惑。我已经尝试了我能找到的所有语法,并且在每种情况下尝试使用 $_ 都会引发相同的错误。如果我只是使用任何其他变量,例如 $test=$_,并在查询中替换它,它就可以正常工作。

认为使用管道运算符可能会出现一些问题(可能一旦 Get-CASMailbox 开始运行 $_ 中的其他内容)我还尝试使用对象属性(这是原始代码的一部分)。在 get-casmailbox 调用之前添加了以下两行
$ServerInfo = "" | Select-Object Name
$ServerInfo.Name = $_.ToString()

获取 CASMailbox -ResultSize:10 -Filter {ServerName -eq $ServerInfo.Name}

也失败了,同样的错误信息。所以我真的很困惑为什么要使用 $_ 或 $ServerInfo.Name 并将其分配给 $test,然后使用 $test 可以正常工作,但前两种方法会引发错误。在我看来,它们中的每一个都应该工作,因为它们都是字符串。有什么不同?

最佳答案

试试这个:

$MailServerList | Foreach-Object { Get-CASMailbox -Filter "ServerName -eq  '$_'" }

关于powershell - 无法在 Powershell 中使用带有管道变量的 -filter 子句运行 Get-CASMailbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6818937/

相关文章:

regex - 使用 Select-String 匹配多个单行模式并写入输出

powershell:如何以dns格式检索计算机名称

exchange-server - EWS : copy items between mailboxes/accounts

同步到 Exchange 帐户时,iOS 无法更新日历事件

c# - 如何为通过 Exchange Server 发送的邮件设置 MHT-body?

powershell - 如何通过 Powershell 为 Azure 配置 github 等

powershell - 处理cmdlet参数时出错?

c# - 枚举您可以使用 EWS 托管 API 访问的共享邮箱名称

exchange-server - Exchange Web 服务,无法设置约会的 startTimeZone (java api)