PowerShell itself doesn't support remoting. However, the script is
using a .NET RegistryKey object that does support remotely accessing
another machine using the OpenRemoteBaseKey method. Think of it as .NET
picking up the slack for PowerShell
Tom G.
--
Rob Campbell wrote:
> I don't think PS supports remote registry access (yet).
>
> As a workaround, you might be able to use the win32_Process Create
> method to run regedit on the remote machine, export the key, and then
> parse the username out of the .reg file.
>
>
>
> "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.
> > > 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