View Single Post
Old 01-03-2007   #3 (permalink)
Gaurhoth


 
 

Re: Collecting information from Active Directory

Use following 4 lines to retrieve a list of computers in your AD. This will return all computers no matter what OU they are in.

$ldapQuery = "(&(objectCategory=computer))"
$de = new-object system.directoryservices.directoryentry
$ads = new-object system.directoryservices.directorysearcher -argumentlist $de,$ldapQuery
$complist = $ads.findall()

Now iterate through $complist and query the registry on each remote computer for last logged on user:

$complist | % {
$cn = $_.properties['cn']
$rk = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$cn)
$rv = $rk.opensubkey("\Software\Microsoft\Windows NT\CurrentVersion\WinLogon")
$lastuser = $rv.GetValue("DefaultDomainName") + "\" + $rv.GetValue("DefaultUserName")
write-host "$cn - $lastuser"
}

--

gaurhoth
http://gaurhothw.spaces.live.com/


"Glenn Wilson" <GlennWilson@discussions.microsoft.com> wrote in message news:42CD0DB7-6364-4D6E-8A43-E967A501CEF7@microsoft.com...
> Hello All,
>
> I need to get a list of all computers in our Active Directory and the user
> who last logged onto it. Has anyone got a sample script to help with this, or
> at least some parts of it.....
>
> Thanks in advance.

My System SpecsSystem Spec