Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #initialize/configure AD query
  2. $strCategory = "computer"
  3.  
  4. $objDomain = New-Object System.DirectoryServices.DirectoryEntry
  5.  
  6. $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
  7. $objSearcher.SearchRoot = $objDomain
  8. $objSearcher.Filter = ("(objectCategory=$strCategory)")
  9.  
  10. $colProplist = "name"
  11. foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
  12.  
  13. $colResults = $objSearcher.FindAll()
  14. #
  15.  
  16. #where store all the data
  17. $data = @()
  18.  
  19. #where store all the computer
  20. $comp = @()
  21.  
  22. #custom obj with some propietes. this update the data
  23. $myobj = "" | select name
  24.  
  25. #get the info from AD and store into an array
  26. foreach ($objResult in $colResults)
  27. {
  28.     $objComputer = $objResult.Properties;
  29.     $comp += $objComputer.name;
  30. }
  31.  
  32. #bi array where save all the results. a rofl for the size :s
  33. $result = New-Object 'object[,]' 100,999
  34.  
  35. #counter of array
  36. $row = 0
  37. $column = 0
  38.  
  39. #quit flag
  40. $quit = 0
  41.  
  42. #a non-stop loop. when user say stop! :)
  43. while( $quit -eq 0 )
  44.     {
  45.     #get date to a string
  46.     #$date = get-date -format "%h:%m:%s-%d%M%y"
  47.     #$date = $date.ToString()
  48.    
  49.     #add propiety to myobj
  50.     $myobj | Add-Member -type noteproperty -name $column -value @()
  51.  
  52.     foreach ($i in $comp)
  53.     {
  54.         $myobj.name = $i
  55.                
  56.         #check network connection
  57.         if( (Test-Connection -ComputerName $myobj.name -quiet) -eq $True )
  58.         {
  59.             $result[$row,$column] = 'true'
  60.         }
  61.         else
  62.         {
  63.             $result[$row,$column] = 'false'
  64.         }
  65.        
  66.         for( $c = 0; $c -le $column; $c++ ) {
  67.             $myobj.$c = $result[$row,$c]
  68.         }
  69.                
  70.         #increase array[x, ] counter
  71.         $row++
  72.        
  73.         $data += $myobj | Select-Object *
  74.        
  75.         $data | ft
  76.     }
  77.    
  78.     #save to csv
  79.     $data | Export-Csv report_adcomputer_response.csv -notype -force
  80.     #bkp, why not ? :)
  81.     $data | Export-Csv report_adcomputer_response.csv.bkp -notype -force
  82.    
  83.     #increase array[x, ] counter
  84.     $column++
  85.    
  86.     #reset array[ ,x] counter
  87.     $row = 0
  88.    
  89.     #clear the object
  90.     $data = @()
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement