Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

want to ping then check for users loggedin

$
0
0

I'm trying to get this script to work, I want to ping a list of computers and if the computer pings, then run psloggedon; if the computer does not ping, then continue to the next computer, I get this error

ParserError: (:) [], ParentContainsErrorRecordException

$Logfile = "C:\Logfiles\logs\LoggedOnUsers.log"
$Computers = (Get-Content c:\logfiles\loggedOnUsers.txt)

# --- ping the computer
$collection = $()
foreach ($Computer in $Computers)

{   
$status = @{ "ComputerName" = $Computer; "TimeStamp" = (Get-Date -f s) }
    if (Test-Connection $Computer -Count 1 -ea 0 -Quiet)
        {
      (if $status["Results"] = "Up")
        psloggedin
        }
        else
        {
        $status["Results"] = "Down"
        exit
        }   
        New-Object -TypeName PSObject -Property $status -OutVariable ComputerStatus   
        $collection += $ComputerStatus}$collection | Export-$Logfile
       

# --- end ping

# --- start psloggedin

function psloggedin {get-$status["Results"]}

if ($status["Results"] -like "Up")
  {
    Foreach ($Computer in $Computers)
   
    [object[]]$sessions = Invoke-Expression "C:\pstools\PsLoggedon.exe -x -l \\$Computer" |
        Where-Object {$_ -match "AD-ENT"}
       
 
        if ($Sessions.count -ge 1)
       
        {
            LogWrite ("{0} $computer User LoggedIn" –f $Sessions.count,
            $Computer) -ForegroundColor 'Magenta'
        }

        else

        {
            LogWrite ("{0} $computer User NOT Loggedon" -f $sessions.count,
            $Computer) -ForegroundColor 'Green'
       
        }
    }
 
 # --- end Function psloggedin

function LogWrite{
param ([string] $txt)
$DateTime = get-date | select -expand datetime
Add-Content $LogFile -Value "$((Get-Date).DateTime): $txt"
}


Viewing all articles
Browse latest Browse all 8583

Trending Articles