troubleshooting Question

Powershell PSLoggedon Active Directory

Avatar of Xytras1
Xytras1 asked on
Microsoft Legacy OSMicrosoft Server OSPowershell
2 Comments1 Solution1853 ViewsLast Modified:
Trying to create a powershell script that will use the 5 results via get-adcomputer than use psloggedon to check if any users are logged on..

Is there a way to only display computers that do not have any one logged on? Been using link below but I don't think its what I want..



http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/17/use-powershell-to-detect-if-a-workstation-is-in-use.aspx


$Computers = (get-adcomputer -filter * -searchbase 'OU=Computers, DC=xytras,DC=com' -ResultSetSize 5 | select-object -ExpandProperty name)
Foreach ($Computer in $Computers)
{
    [object[]]$sessions = Invoke-Expression "c:\temp\PsLoggedon.exe -x -l \\$Computer" |
        Where-Object {$_ -match '^\s{2,}((?<domain>\w+)\\(?<user>\S+))|(?<user>\S+)'} |
        Select-Object @{
            Name='Computer'
            Expression={$Computer}
        },
        @{
            Name='Domain'
            Expression={$matches.Domain}
        },
        @{
            Name='User'
            Expression={$Matches.User}
        }
    IF ($Sessions.count -ge 1)
    {
        Write-Host ("{0} Users Logged into {1}" –f $Sessions.count,    
            $Computer) -ForegroundColor 'Green'
    }
    Else
    {
        Write-Host ("{0} can be rebooted!" -f $Computer), -Foreground 'Red'
           
    }
 }
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros