Avatar of Xytras1
Xytras1
 asked on

Powershell PSLoggedon Active Directory

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'
           
    }
 }
Microsoft Legacy OSMicrosoft Server OSPowershell

Avatar of undefined
Last Comment
Xytras1

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
footech

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Xytras1

ASKER
Thanks for the response. Went with this instead. Know to open Multiple sessions of psloggedon.....

$Computers = (get-adcomputer -filter * -searchbase 'OU=Computers,DC=xytras,DC=com' -ResultSetSize 3 | select-object -ExpandProperty name)
Foreach ($Computer in $Computers)
    {
        Invoke-Expression "c:\temp\PsLoggedon.exe -x -l \\$Computer" | Where-Object {$_ -contains 'No one is logged on locally.'}  | Select-Object $Computer | out-file -Append C:\temp\comzp.txt
    }
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck