Private/Get-LoggedInStatus.ps1


<#
.SYNOPSIS
    Retrieves the logged-in status of a user on a specified remote computer.
 
.DESCRIPTION
    This function checks if any user is currently logged into the specified computer. It uses WMI to retrieve user login status and outputs whether a user is logged in or not.
 
.PARAMETER ComputerName
    The name of the computer to check the login status of.
 
.EXAMPLE
    Get-LoggedInStatus -ComputerName "Server01"
    Checks if any user is logged in on "Server01".
 
.NOTES
    Requires network connectivity and appropriate permissions to access WMI on the target computer.
#>


Function Get-LoggedInStatus {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$ComputerName
    )

    if (Test-Connection -ComputerName $ComputerName -Count 2 -Quiet) {
        try {
            $userDetails = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName -ErrorAction Stop
            if ($userDetails.UserName) {
                Write-Output "Computer in use."
            } else {
                Write-Output 'Not logged on.'
            }
        } catch {
            Write-Output "Failed to retrieve user information: $_"
        }
    } else {
        Write-Output 'Computer offline.'
    }
}