Iz3k34l
(Just in Town)
2016-04-04 09:51 PM
List domain computers

is there a way to query the domain for a list of domain computers?
Ultimately i want to rename a computer and join it to a domain.
I want to be able to step through computer names pc1, pc2 by verifying the computer is not already on the domain or the computer name inst already being used, then naming the next sequential number.


Thanks


Glenn BarnasAdministrator
(KiX Supporter)
2016-04-05 02:13 AM
Re: List domain computers

Welcome to KORG!!

I had to search the archives folder for this, but it's simple and effective:
 Code:
Break on

; Array of domains to query
$aDomains = "@DOMAIN", ''


For Each $Domain in $aDomains
 If $Domain
   "Processing " + $Domain ?

  $aList = GetMachineList($Domain)

  1 + Ubound($aList) " records found." ?
  For Each $Target in $aList
    $Target ?
  Next
 EndIf
Next




Function GetMachineList($DomainName)
	
  Dim $Machines[0], $iX
	
	
  ;Create domain object
  $Domain = GetObject("WinNT://" + $DomainName + ",domain")
	
  ;Set container object equal to domain object.
  $Container = $Domain
	
  ;Iterates through EVERY object in the domain.  Those
  ;objects that match the class are included in the array.
  $iX = -1
  For Each $Computer In $Container
	  
    If $Computer.Class = "Computer"
      $iX = $iX + 1
      ReDim Preserve $Machines[$iX]
      $Machines[$iX] = $Computer.Name
    EndIf
	  
  Next	

  ; Return the $Machines array...
  $GetMachineList = $Machines
	
  ;Object cleanup
  $Domain = 0
  $Container = 0
	
EndFunction
Glenn


Glenn BarnasAdministrator
(KiX Supporter)
2016-04-05 02:15 AM
Re: List domain computers

Look at the AScan() function - once the above code returns the list of computer names, you can use AScan() to search for the new name. If the returned value is -1, it doesn't exist in the array, any other value represents it's index in the array.

Glenn


Iz3k34l
(Just in Town)
2016-04-07 06:18 PM
Re: List domain computers

ok, thanks so much.. i got it to compare and to read in a CSV list, strip out the comma, and compare but now i want to limit the return of the first -1 it finds because as it stand it return the last...
 Code:
For Each $Target in $aData[0]
   if (ASCAN($aList,$Target,,,1) < 0)   
   ? $Target ?
   $rTurn = $Target
   
   EndIF
   ;? $Target
Next

? $rTurn



Thanks


Iz3k34l
(Just in Town)
2016-04-08 11:06 PM
Re: List domain computers

Ok so on to another note, i need to be able to search the domain for a list of computers already on the domain BEFORE being joined to the domain ;o)... i got the script to search for systems within the domain, return a value (not exactly the way i wanted but for testing it will do) and rename the system with the returned value... after a reboot it gave me a domain trust error... any ideas on how to get the computer list from the domain BEFORE being added to the domain?

Thanks


LonkeroAdministrator
(KiX Master Guru)
2016-04-09 04:18 AM
Re: List domain computers

 Code:
GetObject("WinNT:").OpenDSObject("WinNT://MYDOMAIN",altUser, altPwd, 1)


Iz3k34l
(Just in Town)
2016-04-10 02:33 AM
Re: List domain computers

Thanks Glenn and Lonkero; here is the updated code that will get a list of domain computers without being on a domain computer

 Code:
Function GetMachineList($DomainName, $DomainUser, $DomianPass)
	
  Dim $Machines[0], $iX
	
	
  ;Create domain object
  $Domain = GetObject("WinNT:").OpenDSObject("WinNT://$DomainName" , $DomainUser, $DomainPass, 1)
	
  ;Set container object equal to domain object.
  $Container = $Domain
	
  ;Iterates through EVERY object in the domain.  Those
  ;objects that match the class are included in the array.
  $iX = -1
  For Each $Computer In $Container
	  
    If $Computer.Class = "Computer"
      $iX = $iX + 1
      ReDim Preserve $Machines[$iX]
      $Machines[$iX] = $Computer.Name
    EndIf
	  
  Next	

  ; Return the $Machines array...
  $GetMachineList = $Machines
	
  ;Object cleanup
  $Domain = 0
  $Container = 0
	
EndFunction


Glenn BarnasAdministrator
(KiX Supporter)
2016-04-10 05:19 PM
Re: List domain computers

Great! And we especially appreciate following up with your code - I'm sure it will help others!

Glenn