Powershell – I try to list computers that have a specific string in their description, but I'm unable to create a proper command

huangapple go评论52阅读模式
英文:

Powershell - I try to list computers that have a specific string in their description, but I'm unable to create a proper command

问题

在我理解中,我编写了一段代码,首先列出了计算机及其所有属性,然后仅选择描述属性,之后筛选计算机以确定它们是否包含指定的字符串。

Get-ADComputer -Properties * | Select-Object Description | Where-Object { $_.Description -like '*Name*' }

我收到了这个错误信息:

Where-Object:在参数 'Property' 上无法验证参数。参数为 null 或为空。请提供一个不为 null 或为空的参数,然后再尝试运行该命令。

我尝试了多个版本的此代码,就我目前的技能而言,例如:

Get-ADComputer -Properties * | Where-Object { $_.Description -like '*Name*' } | Select-Object Description

或者我完全省略了选择部分。

Get-ADComputer -Properties * | Where-Object { $_.Description -like '*Name*' }

我只是刚刚开始了解PowerShell,坦率地说,我卡住了。我想请您检查我的代码并告诉我有什么问题或需要更改的地方。最好是一个简单的解决方案,就像我上面的代码一样。

提前感谢您!

英文:

In my understanding I made a code that first lists computers with all their properties, then selects only the description property and after that it filters the computers whether they contain the specified string.

Get-ADComputer -Properties * | Select-Object Description | Where $_.Description -Contains "Name"

I received this error:
Where-Object : Cannot validate argument on parameter 'Property'. The argument is null or empty. Provide an argument that is not null
or empty, and then try the command again.

I tried multiple versions of this code on par of my skill for now, such as:
Get-ADComputer -Properties * | Where $_.Description -Contains "Name" (Select-Object Description)

Or I completely omitted the Select part.
Get-ADComputer -Properties * | Where $_.Description -Contains "Name"

I'm only starting to get to know Powershell and to be completely honest I'm stuck. I would like to ask you to inspect my code and tell me what is wrong or should be changed. Preferably a simple solution, as in "one liner" like my code above.

Thanks in advance!

答案1

得分: 0

这应该可以通过提前筛选来完成

Get-ADComputer -Filter 'Description -like "*Name*"' -Properties *
英文:

This should do it by filtering upfront

Get-ADComputer -Filter 'Description -like "*Name*"' -Properties *

huangapple
  • 本文由 发表于 2023年7月20日 15:10:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定