Windows PowerShell function that queries Active Directory when the last time a pc was connected to the network
Posted by: bruntech in PowerShellCopy and Paste this function into notepad. Then insert your domain information in.. LDAP://[your full domian name]/dc=[your sub domain],dc=[your domain],dc=com. for exampele: LDAP://office.test.com/dc=test,dc=office,dc=com
Then copy from notepad and paste into the powershell console window.
function get-lastCompLogon([string] $compName)
{
$computerName = $compName
if($computerName){$input = $computerName}
$srchRoot = new-object system.directoryservices.directoryentry “LDAP://[your full domian name]/dc=[your sub domain],dc=[your domain],dc=com”
$srchr = new-object system.directoryservices.directorysearcher $srchRoot
$srchr.Filter = “(&(objectclass=computer)(Name=$input*))”
$srchr.pagesize = 10000
$srchr.PropertiesToLoad.Clear()
$sr = $srchr.FindOne()
if($sr -ne $null)
{
$computerName + ” last connected to the network ” + “: `t” + ([DateTime]::FromFileTimeUtc([long]$sr.Properties.lastlogontimestamp[0]).ToLocalTime()).ToString()
}
}
Press enter twice .
Then type: get-lastcomplogon computerNamecomputername should be replaced by the name of the host in question for example:get-lastcomplogon testpcshould return somthing that looks like:testpc last connected to the network : 11/05/2007 11:02:30 AM
Tags: PowerShell
Entries (RSS)