Posted On March 31, 2019

Show Users Logons per Computer Names

kimconnect 0 comments
blog.KimConnect.com >> Codes , Windows >> Show Users Logons per Computer Names
# This script is to obtain log on information from a set of target computernames and/or IP addresses.

# Update this value
$ComputerList = "\\FILESERVER01\IT\scripts\computerlist.txt"

function CheckForWMI {
#The line below checks for WMI, if no WMI then the command failed, which makes $? = false. The out-null kills any screen output.
trap {; Continue};
$Script:Win32_ComputerSystem = GWMI Win32_ComputerSystem -ea SilentlyContinue -computer $computername
if ($? -eq $False) {$Script:HasWMI = $FALSE} else {$Script:HasWMI = $TRUE}
}

function Get-ComputerLoggedOnCurrently {
$Script:ComputerLoggedOnCurrently = $Win32_ComputerSystem.UserName;
if ($ComputerLoggedOnCurrently -eq "") {$ComputerLoggedOnCurrently = "---"}
}

function TwoHalfLines {1..2 | % {for ($i=0;$i -lt $width/2; $i++) {Write-host -f cyan "-" -nonewline;};write-host "";} }

function Get-IPorComputerNameFromDNS {
param($computer)
if ($computer -match "^\d" ) {
$Script:ip = $computer
trap {; continue}; $Script:DnsResult = [System.Net.Dns]::GetHostByAddress($Computer)
trap {; continue}; $Script:ComputerName = $DnsResult.HostName.split(".")[0]
} else {
$Script:ComputerName = $computer
trap {; continue}; $Script:DnsResult = [System.Net.Dns]::GetHostByName($Computer)
trap {; continue}; $Script:ip = $DnsResult.AddressList[0].IpAddressToString
}
}

function ProcessListOfComputers {
$ComputerName = $null;
$Script:ComputerNameList = $(get-content $ComputerList)
$Script:ComputerNameListCount = $ComputerNameList.Length
$Script:ComputerListNumber = 0;
$ComputerName = $ComputerNameList[$ComputerListNumber]
if ($ComputerName -eq "") {
do {
$Script:ComputerListNumber++
$Script:ComputerName = $ComputerNameList[$ComputerListNumber]
} until ($ComputerName -ne $null -or $ComputerName -eq "EOF")
}
if ($ComputerName -eq "EOF") {QuitScript "No computers listed in the imported file." "red" "white"}
Get-IPorComputerNameFromDNS $ComputerName
}

function NextIP {
$Script:ComputerListNumber++
$Script:ComputerName = $ComputerNameList[$ComputerListNumber]
if ($ComputerListNumber -eq $ComputerNameListCount -or $ComputerName -eq "EOF") {$Script:stop = $TRUE}
Get-IPorComputerNameFromDNS $ComputerName
}

# Execution begins
Clear-Host
if ((Test-Path $ComputerList) -eq $FALSE) {Add-Content -Path $ComputerList -value "`nEOF"}
$ping = New-Object System.Net.NetworkInformation.Ping
$width = $Host.UI.RawUI.BufferSize.width
clear-host
TwoHalfLines
ProcessListOfComputers
do {
$Script:ComputerLoggedOnCurrently = $null;
Write-Host -f yellow "IP address:`t`t`t`t" -nonewline; Write-Host -f green "$ip";
$Processed_IP = $ip
Write-Host -f yellow "Reading information on computer:`t" -nonewline; write-host -f green "$ComputerName";
$ProcessedComputerName = $computername
$PingReply = $ping.Send($ip)
if ($PingReply.status -eq "Success") {
CheckForWMI; start-sleep -m 300
if ($HasWMI -eq $TRUE) {
Get-ComputerLoggedOnCurrently
Write-Host -f yellow "Logged On Currently:`t`t" -nonewline; Write-host -f green "`t$ComputerLoggedOnCurrently`n"
$computername = $null
NextIP
} else {write-host -f red "`tUnable to access WMI. Computer may not have WMI, or`n`tscript was unable to get a computername from DNS."; $ComputerLoggedOnCurrently = "N/A - WMI not accessible."; NextIP }
} else {Write-host -f red "`tCan not ping $ComputerName-$ip."; $ComputerLoggedOnCurrently = "N/A - Computer not pingable."; NextIP }

if ($Results -eq $null) {$Results = ,("$Processed_IP","$ProcessedComputerName","$ComputerLoggedOnCurrently")} `
else {$Results += ,("$Processed_IP","$ProcessedComputerName","$ComputerLoggedOnCurrently")}
} until ($stop -eq $TRUE)
TwoHalfLines
1..4 | % {write-host "`n"}
write-host -f green "Computer Name`t`tIP Address`t`tLogged on"
foreach ($line in $Results) {
write-host -f yellow "$($line[1])`t`t`t$($line[0])`t`t$($line[2])"
}
1..5 | % {write-host "`n"}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Check TCP Connections of Server by Port Numbers

# Check-TCP-Connections.ps1# This function will output progress onto the console as well as returning a…

Office 365 Active Directory Hybrid Accounts Administration

Intro This scenario assumes that Azure AD Sync has been used to synchronize on premise…

Linux: Creating Soft Links as Directories

Optional test: create a soft-link for directory as hard-links are not allowed source=/nfs-share/linux03/docker/containers destinationdirectory=/var/lib/docker sudo…
Other project: DragonCoin.com