Last active
March 30, 2018 00:24
-
-
Save lazywinadmin/c7fabbc7ba0a69a2842cef95288ded08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-DomainComputer { | |
[CmdletBinding()] | |
PARAM( | |
[Parameter(ValueFromPipelineByPropertyName=$true, | |
ValueFromPipeline=$true)] | |
[Alias("Computer")] | |
[String[]]$ComputerName, | |
[Alias("ResultLimit","Limit")] | |
[int]$SizeLimit='100', | |
[Parameter(ValueFromPipelineByPropertyName=$true)] | |
[Alias("Domain")] | |
[String]$DomainDN=$(([adsisearcher]"").Searchroot.path), | |
[Alias("RunAs")] | |
[System.Management.Automation.Credential()] | |
[PSCREDENTIAL] | |
$Credential = [System.Management.Automation.PSCredential]::Empty | |
)#PARAM | |
PROCESS{ | |
IF ($ComputerName){ | |
FOREACH ($item in $ComputerName){ | |
TRY{ | |
# Building the basic search object with some parameters | |
Write-Verbose -Message "COMPUTERNAME: $item" | |
$Searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher ` | |
-ErrorAction 'Stop' -ErrorVariable ErrProcessNewObjectSearcher | |
$Searcher.Filter = "(&(objectCategory=Computer)(name=$item))" | |
$Searcher.SizeLimit = $SizeLimit | |
$Searcher.SearchRoot = $DomainDN | |
# Specify a different domain to query | |
IF ($PSBoundParameters['DomainDN']){ | |
IF ($DomainDN -notlike "LDAP://*") {$DomainDN = "LDAP://$DomainDN"}#IF | |
Write-Verbose -Message "Different Domain specified: $DomainDN" | |
$Searcher.SearchRoot = $DomainDN}#IF ($PSBoundParameters['DomainDN']) | |
# Alternate Credentials | |
IF ($PSBoundParameters['Credential']) { | |
Write-Verbose -Message "Different Credential specified: $($Credential.UserName)" | |
$Domain = New-Object -TypeName System.DirectoryServices.DirectoryEntry ` | |
-ArgumentList $DomainDN,$($Credential.UserName),$($Credential.GetNetworkCredential().password) ` | |
-ErrorAction 'Stop' -ErrorVariable ErrProcessNewObjectCred | |
$Searcher.SearchRoot = $Domain}#IF ($PSBoundParameters['Credential']) | |
# Querying the Active Directory | |
Write-Verbose -Message "Starting the ADSI Search..." | |
FOREACH ($Computer in $($Searcher.FindAll())){ | |
Write-Verbose -Message "$($Computer.properties.name)" | |
New-Object -TypeName PSObject -ErrorAction 'Continue' ` | |
-ErrorVariable ErrProcessNewObjectOutput -Property @{ | |
"Name" = $($Computer.properties.name) | |
"DNShostName" = $($Computer.properties.dnshostname) | |
"Description" = $($Computer.properties.description) | |
"OperatingSystem"=$($Computer.Properties.operatingsystem) | |
"WhenCreated" = $($Computer.properties.whencreated) | |
"DistinguishedName" = $($Computer.properties.distinguishedname)}#New-Object | |
}#FOREACH $Computer | |
Write-Verbose -Message "ADSI Search completed" | |
}#TRY | |
CATCH{ | |
Write-Warning -Message ('{0}: {1}' -f $item, $_.Exception.Message) | |
IF ($ErrProcessNewObjectSearcher){ | |
Write-Warning -Message "PROCESS BLOCK - Error during the creation of the searcher object"} | |
IF ($ErrProcessNewObjectCred){ | |
Write-Warning -Message "PROCESS BLOCK - Error during the creation of the alternate credential object"} | |
IF ($ErrProcessNewObjectOutput){ | |
Write-Warning -Message "PROCESS BLOCK - Error during the creation of the output object"} | |
}#CATCH | |
}#FOREACH $item | |
}#IF | |
}#Process | |
}#function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment