Product, version and build:
(*** Please do not write "latest" as a version, specify a version number ***)
32 or 64 bit version of product:
Operating system: win 7+
32 or 64 bit OS:both
PowerShell Version:4
I want to write a simple gui that will become a compiled exe that checks if a computer name entered is in AD
the pc's will have powershell 7 on win 7 and newer. Get-adcomputer seems to need rsat installed which it wont be
what is the easiest way to see if a pc is in a given AD? is there a good alternative to get-adcomputer?
im new so any help would be great
DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
using get-adcomputer or an alternative
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- ocsscott6969
- Posts: 48
- Last visit: Tue Dec 05, 2023 10:04 am
Re: using get-adcomputer or an alternative
[TOPIC MOVED TO THE POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: using get-adcomputer or an alternative
You can use this DirectorySearcher here, which is a query to search your Active Directory for a specific item, in this case it is "objectCategory=computer", your Searchstring is then $computername
Replace <DOMAIN> with your domain LDAP Path e.g.: "DC=contose,DC=COM"
Just an example for your task.
Start with creating a GUI and use a button + textbox, on button press use text from Textbox and start that function replace <computername> with your "Textbox.Text"
Greetings,
Raiz
Replace <DOMAIN> with your domain LDAP Path e.g.: "DC=contose,DC=COM"
- function findADPC
- {
- param
- (
- [System.String]$computer
- )
- $Searcher = New-Object DirectoryServices.DirectorySearcher
- $Searcher.SearchRoot = 'LDAP://<DOMAIN>'
- $Searcher.Filter = '(&(objectCategory=computer)(name=' + $computer+ '))'
- [System.Array]$result = $Searcher.FindAll()
- if(($result | Measure-Object).count -gt 0)
- {
- write-host "Computer found"
- return $true
- }
- else
- {
- return $false
- }
- }
- findADPC -computer "<Computername>"
Start with creating a GUI and use a button + textbox, on button press use text from Textbox and start that function replace <computername> with your "Textbox.Text"
Greetings,
Raiz
Re: using get-adcomputer or an alternative
A little shortcut. We can use a much simpler filter. Also a function is not really necessary as it takes only one line to test for exisitence.
If you want to grab the computer object then do this:
- #
- if(([adsisearcher]"samaccountname=$computer$").FindOne()){
- write-host "Computer found"
- } else {
- write-host "Computer not found"
- }
- #
- if(($c = [adsisearcher]"samaccountname=$computer$").FindOne()){
- write-host "Computer found"
- $c
- } else {
- write-host "Computer not found"
- }