Get-ADCompter 命令返回空响应
我试图将字符串和 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 技术交流群。

发布评论
评论(2)
·深蓝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
因为您没有在查询中使用任何通配符。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
问题在于字符串扩展如何工作以及如何使用它。现在您拥有:
一旦您输入这些变量的值,它就会这样显示:
您可以通过多种方法来纠正此问题。第一种方法是简单地从字符串中删除
+
,使其内容如下:第二种方法是放入如下子表达式:
或者您可以在引用它之前将它们组合起来,如下所示:
The issue is how string expansion works, and how you're using it. Right now you have:
Once you sub in the values for those variables it reads like this:
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:The second way is to put in a subexpression like this:
Or you could combine them before you reference it like: