英文:
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 "Name -like '*$computerName*'"
如果使用-LDAPFilter
:
Get-ADComputer -LDAPFilter "(cn=*$computerName*)"
有关详细信息,请参阅about_ActiveDirectory_Filter。
<details>
<summary>英文:</summary>
<!-- language-all: sh -->
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论