无法在 Exchange 管理控制台中将变量与 Get-User -Filter 一起使用

发布于 2024-09-28 15:57:48 字数 544 浏览 2 评论 0原文

我似乎无法在下面的情况下使用变量。

[PS] C:\>Get-User -Filter {SamAccountName -eq "Test.Smith"}
Name                                                        RecipientType
----                                                        -------------
Test Smith                                                  UserMailbox

[PS] C:\>$SamAccountName = "Test.Smith"
[PS] C:\>Get-User -Filter {SamAccountName -eq $SamAccountName}
[PS] C:\>echo $SamAccountName
Test.Smith
[PS] C:\>

您可以看到当我键入名称时该命令运行良好,但当我使用变量时则不然。谢谢!

I cannot seem to use variable in the situation below.

[PS] C:\>Get-User -Filter {SamAccountName -eq "Test.Smith"}
Name                                                        RecipientType
----                                                        -------------
Test Smith                                                  UserMailbox

[PS] C:\>$SamAccountName = "Test.Smith"
[PS] C:\>Get-User -Filter {SamAccountName -eq $SamAccountName}
[PS] C:\>echo $SamAccountName
Test.Smith
[PS] C:\>

You can see the command works fine when I type out the name, but not when I use a variable. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情空心 2024-10-05 15:57:48

我无权访问此 cmdlet,您确定它需要脚本块而不是字符串吗?如果它需要一个字符串,请尝试以下操作:

Get-User -Filter "SamAccountName -eq $SamAccountName"

如果它确实需要一个脚本块,请尝试:

Get-User -Filter {SamAccountName -eq $SamAccountName}.GetNewClosure()

I don't have access to this cmdlet, are you sure it takes a scriptblock and not a string? If it takes a string try this:

Get-User -Filter "SamAccountName -eq $SamAccountName"

If it really takes a scriptblock try:

Get-User -Filter {SamAccountName -eq $SamAccountName}.GetNewClosure()
指尖上的星空 2024-10-05 15:57:48

如注释中所示,请在变量周围添加单引号,否则您的过滤结果的语法不正确。

 Get-User -Filter "SamAccountName -eq '$SamAccountName'"

直接传递参数时,只需传递变量即可。但在本例中,您正在构建格式正确的查询字符串,单引号是其中的一部分。

当您获得完整答案时,不要将其保留为评论...将其创建为完整答案。

As seen in the comments, add single quotes around the variables, or your filter result has incorrect syntax.

 Get-User -Filter "SamAccountName -eq '$SamAccountName'"

When passing parameters directly you can just pass the variable. But in this case you are building a properly formatted query string, and the single quotes are part of that.

When you get a full answer, don't leave it as a comment... create it as a full answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文