View Single Post
Old 01-04-2007   #6 (permalink)
Tom G.


 
 

Re: Collecting information from Active Directory

My guess would be that you're receiving this error because the machine
you're trying to open the registry on is not currently available or is
not accessible for some other reason (security, firewall, etc). You can
tell the script to continue by either trapping the error or just
setting the global $ErrorActionPreference variable to
"SilentlyContinue" before running the script. This way, when the script
encounters a machine that it cannot reach it will just move onto the
next machine.

Tom G.
--



Glenn Wilson wrote:

> Thanks parts of it work, but I am geting the followig error with the
> script, it runs collects the machines, then when it goes to read the
> registery I get the following.
>
> Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The
> network path was not found."
> At E:\PowerShell\Scripts\get-computerlastlogon.ps1:8 char:59
> + $rk = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey( <<<<
> [Microsoft.Win32.RegistryHive]::LocalMachine,$cn)
> You cannot call a method on a null-valued expression.
> At E:\PowerShell\Scripts\get-computerlastlogon.ps1:10 char:29
> + $lastuser = $rv.GetValue( <<<< "DefaultDomainName") + "\" +
> $rv.GetValue("DefaultUserName")
>
>
> "Gaurhoth" wrote:
>
> > 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.Re
> > gistryHive]::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