8^}
@SimonB
8^}
That was not what my suggestion was, but just highlighting that fact you can get all this without all the calculated property stuff. However, yes, in a large enterprise, with thousands of machines, multiple domains, etc. Sure, very, very bad thing. No different than using Server Manager and adding every server in the domain to the ‘All Servers’ node. You only add what you need of course, thus you only select what you need for any script / automation effort.
And @BGomes is already doing…
‘$colResults = $objSearcher.FindAll()’
…in the code, so, ostensibly the same thing as…
Get-ADComputer -Filter *
… well, without the -Properties * thing. But you only need to do that for one computer to get the properties you want for your final select thingy.
@BGomes, you don’t add all that, you just add what you need. Again all the calculated stuff is a bit overkill IMHO, but as you did with those, you do the same thing for the IPA.
$computer | Select-Object -Property @{Name='Name';Expression={$_.CN}},@{Name='Owner';Expression={$_.ObjectSecurity.Owner}},@{Name='Created';Expression={$_.whencreated}},@{Name='IPv4Address';Expression={$_.IPv4Address}}
Yet, here’s the thing. If your ‘$computer’ object does not have as part of the properties, you can’t get it from there. So, you have to ask for it another way. Say, like…
@{Name='IPv4Address';Expression={(Test-Connection -ComputerName $_.Name -Count 1).IPV4Address.IPAddressToString}}
Thus, that means you are making two network calls vs one. One to the DC for all the ADDS stuff and a second to ping a box to get the IPA. So, this…
cls
$DomainName = 'LDAP://CN=Computers,DC=contoso,DC=com'
$Root = New-Object DirectoryServices.DirectoryEntry $DomainName
$objSearcher = New-Object DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $Root
$objSearcher.SearchScope = "SubTree"
$ADResults = @()
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$computer = $objResult.GetDirectoryEntry()
#$computer.nTSecurityDescriptor
$computer | Select-Object -Property @{Name='Name';Expression={$_.CN}},
@{Name='Owner';Expression={$_.ObjectSecurity.Owner}},
@{Name='Created';Expression={$_.whencreated}},
@{Name='IPv4Address';Expression={(Test-Connection -ComputerName $_.Name -Count 1).IPV4Address.IPAddressToString}}
}
# Results
Name Owner Created IPv4Address
---- ----- ------- -----------
SVR01 contoso\Domain Admins 4/2/2017 6:42:01 AM 192.168.1.1
...
Sure, it works, but why take the extra step, then there is this effort also being looked on as bad by your network security admins as potential ping sweep, thus firing off warnings to them, if they are monitoring this sort of activity. So, there’s that.
Again, Get-ADComputer is just far simpler and less work, easier to maintain and follow, long term and no ping sweeps needed.