Friday, October 14, 2022

Powershell function to lookup a single computer by ComputerName

This is a short function to create a function called "Show-Computer".


#
# Show a single computer
#
function Show-Computer
{
    param (
        [Parameter(Mandatory)] $Computername
        )

    try{
        $test = Get-ADComputer -Identity $Computername -Properties CanonicalName, CN, Description, Created, IPv4Address, MemberOf, OperatingSystem, OperatingSystemVersion,LastLogontimestamp,LastLogonDate -ErrorAction Stop |
            Select CanonicalName, CN, Description, Created, Enabled, IPv4Address, MemberOf, Name, ObjectClass, OperatingSystem, OperatingSystemVersion, LastLogonDate, @{n="lastLogontimestamp";e={[datetime]::FromFileTime($_.lastLogontimestamp)}}
        if ($test) {
            Write-Output $test
        }
    } catch {
        Write-Output "Computer object $Computername does not exist in AD"
    }
}

Execute the powershell script to create the command, then you should be able to use it in powershell like this:


C:\windows\system32> Show-Computer -Computername XZY

Hope you find that useful.

No comments:

Post a Comment