using get-adcomputer or an alternative

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
This topic is 6 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ocsscott6969
Posts: 48
Last visit: Tue Dec 05, 2023 10:04 am

using get-adcomputer or an alternative

Post by ocsscott6969 »

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
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: using get-adcomputer or an alternative

Post by davidc »

[TOPIC MOVED TO THE POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
User avatar
r41z0r
Posts: 8
Last visit: Sun Mar 04, 2018 10:36 pm

Re: using get-adcomputer or an alternative

Post by r41z0r »

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"
  1. function findADPC
  2. {
  3.     param
  4.     (
  5.         [System.String]$computer
  6.     )
  7.     $Searcher = New-Object DirectoryServices.DirectorySearcher
  8.     $Searcher.SearchRoot = 'LDAP://<DOMAIN>'
  9.     $Searcher.Filter = '(&(objectCategory=computer)(name=' + $computer+ '))'
  10.     [System.Array]$result = $Searcher.FindAll()
  11.     if(($result | Measure-Object).count -gt 0)
  12.     {
  13.         write-host "Computer found"
  14.         return $true
  15.     }
  16.     else
  17.     {
  18.          return $false  
  19.     }
  20. }
  21.  
  22. findADPC -computer "<Computername>"
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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: using get-adcomputer or an alternative

Post by jvierra »

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.
  1. #
  2.     if(([adsisearcher]"samaccountname=$computer$").FindOne()){
  3.         write-host "Computer found"
  4.     } else {
  5.         write-host "Computer not found"
  6.     }
If you want to grab the computer object then do this:
  1. #
  2.     if(($c = [adsisearcher]"samaccountname=$computer$").FindOne()){
  3.         write-host "Computer found"
  4.         $c
  5.     } else {
  6.         write-host "Computer not found"
  7.     }
This topic is 6 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked