Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Get list of servers in the AD
- $computers = Get-ADComputer -Filter {OperatingSystem -like "*server*"} -Properties OperatingSystem
- $ComputerList = foreach ($Computer in $computers){
- #check if the computer is online.
- $PingResult = Test-NetConnection -ComputerName $computer.DNSHostName -InformationLevel Quiet -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
- if ($PingResult){
- #If online try to get the memership data
- try {
- $ADSIComputer = [ADSI]("WinNT://$($Computer.DNSHostName),computer")
- $group = try{
- $ADSIComputer.psbase.children.find('Administrators', 'Group')
- }
- Catch{
- #Add custom name of the Administrators group here.
- $ADSIComputer.psbase.children.find('Administrators', 'Group')
- }
- $members = $group.psbase.Invoke("members") | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
- }
- Catch {
- #Leave an emtpy entry on any errors while getting the membership data.
- $members = ""
- }
- }
- else{
- $members = ""
- }
- #Move on each member and create its enty, this helps formatting the data for Pivot tables in Excel.
- foreach($member in $members){
- #Check if the member is an AD object, CanonicalName for OU filtering in Excel.
- if($member -ne "") {$ADObject = Get-ADObject -Filter {SamAccountName -eq $member} -Properties CanonicalName}
- else {$ADObject = $null}
- if($ADObject)
- {
- $DomainMember = $True
- $ObjectType = $ADObject.ObjectClass
- $OU = $ADObject.CanonicalName.replace("/$($ADObject.Name)","") #removes the Full Name form CanonicalName
- }
- else{
- $DomainMember = $false
- $ObjectType = ""
- $OU = ""
- }
- [PSCustomObject]@{
- ComputerName = $computer.DNSHostName
- PingResult = $PingResult
- OperatingSystem = $Computer.OperatingSystem
- DomainMember = $DomainMember
- ObjectType = $ObjectType
- OU = $OU
- MemberName = $member
- }
- }
- }
- $ComputerList | ogv #You can change this to save as CSV or any other format.
Add Comment
Please, Sign In to add comment