无法使用带有管道变量的 -filter 子句在 Powershell 中运行 Get-CASMailbox
我在使用 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 :无法将参数“Filter”绑定到目标。异常设置“Filter”:“无法转换类型为‘System.Management.Automa’的对象 tion.PSObject' 来键入 'System.String'。” 在 C:\Users\erawlins\Desktop\MailboxFilterBug.ps1:7 字符:38 + Get-CASMailbox -ResultSize:10 -Filter <<<<< {服务器名称-eq $_} + CategoryInfo : WriteError: (:) [Get-CASMailbox],ParameterBindingException + FullQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.GetCASMailbox
我对此感到非常困惑。我已经尝试了我能找到的所有语法,并且在每种情况下尝试使用 $_ 都会引发相同的错误。如果我只是任何其他变量,例如 $test=$_,并在查询中替换它,它就可以正常工作。
认为使用管道运算符可能会出现一些问题(也许一旦 Get-CASMailbox 开始运行 $_ 中的其他内容),我还尝试使用对象属性(这是原始代码的一部分)。 之前添加以下两行
$ServerInfo = "" | Select-Object Name
$ServerInfo.Name = $_.ToString()
在 get-casmailbox 调用Get-CASMailbox -ResultSize:10 -Filter {ServerName -eq $ServerInfo.Name}
也失败,相同的错误消息。所以我真的很困惑为什么采用 $_ 或 $ServerInfo.Name 并将其分配给 $test,然后使用 $test 工作正常,但前两种方法会抛出错误。在我看来,这些都应该起作用,因为它们都是字符串。有什么区别?
I'm having some trouble with the syntax of Exchange cmdlet Get-CASMailbox.
I'm running in a 2007 environment and I've tested in PowerGUI 2.4, 3.0, and the PS ISE 2.0.
Sample code for demonstration purposes:
[String[]] $MailServerList = @('IP-0A207B07')
$MailServerList | %{
Get-CASMailbox -ResultSize:10 -Filter {ServerName -eq $_}
}
In this example there is only a single mailserver. During the call to Get-CASMailbox the pipeline operator $_ is IP-0A207B07 and $_.GetType() confirms it is a string. If this line runs I get the following error:
Get-CASMailbox : Cannot bind parameter 'Filter' to the target. Exception setting "Filter": "Unable to cast object of type 'System.Management.Automa
tion.PSObject' to type 'System.String'."
At 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
I'm really confused by this. I've tried every syntax I could find and in every case attempting to use $_ throws the same error. If I just any other variable, such as $test=$_, and substitute that in the query it works fine.
Thinking there could be some problems with using the pipeline operator (maybe once Get-CASMailbox starts running something else is in $_) I also tried using an object property (which is part of the original code). The following two lines were added before the get-casmailbox call
$ServerInfo = "" | Select-Object Name
$ServerInfo.Name = $_.ToString()
Get-CASMailbox -ResultSize:10 -Filter {ServerName -eq $ServerInfo.Name}
Fails as well, same error message. So I'm really confused why taking $_ or $ServerInfo.Name and assigning it to $test, then using $test works fine but the first two methods throw an error. It seems to me that each of these should work as they're all strings. What's the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
您将脚本块传递给过滤器参数,并且引用自动变量 ($)。当脚本块被评估时,它可能位于定义 $ 的其他代码中。在嵌套作用域中使用自动变量时请务必小心。
You are passing a scriptblock to the filter parameter and you are referencing an automatic variable ($). By the time the scriptblock gets evaluated, it could be inside some other code that is defining $. Always be careful while using automatic variables with nested scopes.