查找Active Directory中“喜欢”的计算机的脚本

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

Script for finding "Like" PCs in Active Directory

问题

I'm trying to write a script for prompting a User to get info on computers based in AD. It works without the Filter being applied. But when adding the filter the code breaks. Is the syntax wrong for the filter? I'm sure it's an easy fix but I am stumped. Yes, this is being run as an administrator.

$computerName = Read-Host -Prompt 'Input the computer Name you are looking for.'
Get-ADComputer -Filter "Name -like $computerName" -Properties IPv4Address | FT Name,DNSHostName,IPv4Address -A
Write-Host "You input Computer $computerName"
英文:

Im trying to write a script for prompting a User to get info on computers based in AD. It works without the Filter being applied. But when adding the filter the code breaks. Is the syntax wrong for the filter? Im sure its an easy fix but i am stummped. Yes this is being ran as administrator.

$computerName = Read-Host -Prompt 'Input the computer Name you are looking for.' 
Get-ADComputer -Filter "Name -like $computerName" -Properties IPv4Address | FT Name,DNSHostName,IPv4Address -A
Write-Host "You input Computer $computerName"


</details>


# 答案1
**得分**: 3

<!-- language-all: sh -->

正确的筛选语法,如果使用-Filter,应该是:

Get-ADComputer -Filter &quot;Name -like &#39;*$computerName*&#39;&quot;

如果使用-LDAPFilter

Get-ADComputer -LDAPFilter &quot;(cn=*$computerName*)&quot;

有关详细信息,请参阅about_ActiveDirectory_Filter


<details>
<summary>英文:</summary>

&lt;!-- language-all: sh --&gt;

The correct syntax for your filter should be, if using `-Filter`:

Get-ADComputer -Filter "Name -like '$computerName'"


And if using `-LDAPFilter`:

Get-ADComputer -LDAPFilter "(cn=$computerName)"


See [__about_ActiveDirectory_Filter__](https://learn.microsoft.com/en-us/previous-versions/windows/server/hh531527(v=ws.10)) for details.

</details>



huangapple
  • 本文由 发表于 2023年2月24日 03:03:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549253.html
匿名

发表评论

匿名网友

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

确定