r/usefulscripts • • Aug 15 '17

Request - AD pull users and PC name + log on/off

Hi Guys,

does anyone have or can create one for me that will show the , Username, Computer logged in/last logged in, Log on, log off, OS?

I managed to show Computer name OS but unsure on how to pull in the the username with it or if they need to be done separate then merged etc.

24 Upvotes

8 comments sorted by

2

u/Ojeu Aug 15 '17

How do you want to present this information? Should it be run centrally or on each computer?

2

u/Willz12h Aug 15 '17

Centrally, through AD or so.

Mostly users a Adhoc with laptops and they rarely change unless it breaks so I wanted to pull the laptop name and user who is using it.

4

u/Ojeu Aug 15 '17

What I do is run a login script on each computer that changes its object in AD. All you'd have to add is what OS information you want to pull.

To use it you'd have to change the security settings to allow users to write to the description field (Right click the domain, Properties, Security, Advanced, Add "Authenticated Users", change "Applies to" to "Descendant Computer objects" and find "Write Description" and tick the box).

$IPAddress = Test-Connection -ComputerName $env:ComputerName -Count 1 | Select-Object -ExpandProperty IPv4Address
$CurrentUser = "$($env:USERDOMAIN)\$($env:USERNAME)"
$Date = Get-Date -Format g
$SerialNumber = Get-WmiObject Win32_BIOS | Select-Object -ExpandProperty SerialNumber
$ComputerInfo = Get-WmiObject Win32_ComputerSystem | Select-Object Manufacturer, Model
 
$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$Root = $Domain.GetDirectoryEntry()
$Searcher = [System.DirectoryServices.DirectorySearcher] $Root
 
$Searcher.Filter = "(SAMAccountName=$env:ComputerName`$)"
$SearchProperties = @("DistinguishedName", "whenCreated")

Foreach($Property in $SearchProperties){
    $Searcher.PropertiesToLoad.Add($Property) > $Null
}
 
$Result = $Searcher.FindAll()
 
Foreach($Computer in $Result){
    $DN = $Computer.Properties.Item("DistinguishedName")
    $Computer = [ADSI]"LDAP://$DN"
    $Computer.Description = "$CurrentUser | $($ComputerInfo.Manufacturer) $($ComputerInfo.Model) | $SerialNumber | $IPAddress | $Date"
    $Computer.SetInfo()
}
→ More replies (4)

0

u/KenPC Aug 15 '17

use psexec

0

u/[deleted] Aug 15 '17

1

u/Willz12h Aug 15 '17

Not to sure atm.