Get-ADCompter 命令返回空响应

发布于 01-11 11:23 字数 463 浏览 3 评论 0原文

我试图将字符串和 int 组合在一起以获得输出 JWang1 但每次运行此命令时它都会返回 null,感谢任何帮助,谢谢

$pcname = "Jwang"
$number = 1
Get-ADcomputer -filter "name -like '$pcname + $number'" | select -ExpandProperty name

目标是获取命令搜索“JWANG1”,但结果不断返回 null

或者,如果我这样做,我会得到一个发回给我的搜索结果。

$pcname = "Jwang1"
Get-ADcomputer -filter "name -like '$pcname'" | select -ExpandProperty name

这里的区别在于,我不是试图将字符串和 int 组合起来,而是如何获取它结合并工作?

I'm trying to combine a string and an int together to get the output JWang1 but everytime I run this command it keeps returning back null, any help is appreciated, thank you

$pcname = "Jwang"
$number = 1
Get-ADcomputer -filter "name -like '$pcname + $number'" | select -ExpandProperty name

The goal is to get the command to search "JWANG1", but the result keeps coming back null

Alternatively if I do this, I get a search result posted back to me

$pcname = "Jwang1"
Get-ADcomputer -filter "name -like '$pcname'" | select -ExpandProperty name

The difference here is that I am not trying to combine the string and int, but how do I get it to combine and work?

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

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

发布评论

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

评论(2

那支青花2025-01-18 11:23:07

问题在于字符串扩展如何工作以及如何使用它。现在您拥有:

"name -like '$pcname + $number'"

一旦您输入这些变量的值,它就会这样显示:

"name -like 'JWANG + 1'"

您可以通过多种方法来纠正此问题。第一种方法是简单地从字符串中删除 + ,使其内容如下:

"name -like '$pcname$number'"

第二种方法是放入如下子表达式:

"name -like '$($pcname + $number)'"

或者您可以在引用它之前将它们组合起来,如下所示:

$pcname = "Jwang"
$number = 1
$combinedname = $pcname + $number
Get-ADcomputer -filter "name -like '$combinedname'" | select -ExpandProperty name

The issue is how string expansion works, and how you're using it. Right now you have:

"name -like '$pcname + $number'"

Once you sub in the values for those variables it reads like this:

"name -like 'JWANG + 1'"

There's several ways you could go about correcting this. The first is to simply remove the + from the string so it reads like this:

"name -like '$pcname$number'"

The second way is to put in a subexpression like this:

"name -like '$($pcname + $number)'"

Or you could combine them before you reference it like:

$pcname = "Jwang"
$number = 1
$combinedname = $pcname + $number
Get-ADcomputer -filter "name -like '$combinedname'" | select -ExpandProperty name
·深蓝2025-01-18 11:23:07

在这种情况下,您需要使用 子表达式运算符 $(..) 来解析串联操作:

$pcname = "Jwang"
$number = 1

"name -like '$pcname + $number'"
# Interpolates both variables without resolving the concatenation
# Results in: name -like 'Jwang + 1'

"name -like '$($pcname + $number)'"
# Resolves the expression inside `$(..)` before interpolating it
# Results in: name -like 'Jwang1'

另外,对 Active Directory 的查询的正确运算符应该是-eq 而不是 -like 因为您没有在查询中使用任何通配符。

In this case you would need to use the Subexpression operator $(..) to resolve the concatenation operation:

$pcname = "Jwang"
$number = 1

"name -like '$pcname + $number'"
# Interpolates both variables without resolving the concatenation
# Results in: name -like 'Jwang + 1'

"name -like '$($pcname + $number)'"
# Resolves the expression inside `$(..)` before interpolating it
# Results in: name -like 'Jwang1'

As aside, the right operator for your query to Active Directory should be -eq instead of -like since you're not using any wildcard for your query.

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