daily pastebin goal
94%
SHARE
TWEET

Untitled

a guest Mar 19th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Get list of servers in the AD
  2. $computers = Get-ADComputer -Filter {OperatingSystem -like "*server*"} -Properties OperatingSystem
  3.  
  4. $ComputerList = foreach ($Computer in $computers){
  5.     #check if the computer is online.
  6.     $PingResult = Test-NetConnection -ComputerName $computer.DNSHostName -InformationLevel Quiet -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
  7.     if ($PingResult){
  8.         #If online try to get the memership data
  9.         try {
  10.             $ADSIComputer = [ADSI]("WinNT://$($Computer.DNSHostName),computer")
  11.             $group = try{
  12.                     $ADSIComputer.psbase.children.find('Administrators', 'Group')
  13.                 }
  14.                 Catch{
  15.                         #Add custom name of the Administrators group here.
  16.                     $ADSIComputer.psbase.children.find('Administrators', 'Group')
  17.                 }
  18.             $members = $group.psbase.Invoke("members") | foreach {$_.GetType().InvokeMember("Name",  'GetProperty',  $null,  $_, $null)}
  19.         }
  20.         Catch {        
  21.             #Leave an emtpy entry on any errors while getting the membership data.
  22.             $members = ""      
  23.         }
  24.     }
  25.     else{
  26.         $members = ""
  27.     }
  28.         #Move on each member and create its enty, this helps formatting the data for Pivot tables in Excel.
  29.     foreach($member in $members){
  30.             #Check if the member is an AD object, CanonicalName for OU filtering in Excel.
  31.         if($member -ne "") {$ADObject = Get-ADObject -Filter {SamAccountName -eq $member} -Properties CanonicalName}
  32.         else {$ADObject = $null}
  33.         if($ADObject)
  34.         {
  35.             $DomainMember = $True
  36.             $ObjectType   = $ADObject.ObjectClass
  37.             $OU = $ADObject.CanonicalName.replace("/$($ADObject.Name)","") #removes the Full Name form CanonicalName
  38.         }
  39.         else{
  40.             $DomainMember = $false
  41.             $ObjectType   = ""
  42.             $OU = ""
  43.         }
  44.  
  45.         [PSCustomObject]@{
  46.             ComputerName    = $computer.DNSHostName
  47.             PingResult      = $PingResult
  48.             OperatingSystem = $Computer.OperatingSystem
  49.             DomainMember    = $DomainMember
  50.             ObjectType      = $ObjectType
  51.             OU              = $OU
  52.             MemberName      = $member
  53.         }
  54.     }
  55.  
  56. }
  57.  
  58. $ComputerList | ogv #You can change this to save as CSV or any other format.
RAW Paste Data
Top