Monday, August 20, 2018

Powershell - last user to logon to a computer

I am just going to say the next few posts are powershell scripts that I have just used in a desktop ADMT project. Some points to note:
  1. I know I can get a collection of all items and execute them at once, I like having exported lists and seeing them click through, it makes me feel like I am in the matrix
  2. I am more of a VB person, so my scripts read like VB
  3. I am sharing these, if I used your script as a part of mine, thanks for your help
  4. These were all put together to scratch my itch, I hope they can help you.
  5. These were all running in W7 (x86, then x64 for Office online) with the latest release of Powershell.
Enjoy.

Script 1:  last user logged on to that computer which is running



$computers = Import-Csv C:\temp\computerlist.txt
ipconfig /flushdns >null
write-host "This Computer, Comp PW last set, User LastLogonDate, User profile name, Userprofile LastWriteTime"

foreach ($computers in $computers){

$ThisComputer=$computers.computername.trim()
#$DNSComputer=$ThisComputer + ".name.gov.au"

$didit = Test-Connection $ThisComputer -count 1 -quiet

if ($didit -ne $false) {

$profile=$null
$profile=Get-ChildItem -Path \\$ThisComputer\c$\users\ -Exclude public, servicedeskxp, wim, sccm_domain, administrator, WIM, colvinda  | Sort-Object LastWriteTime -Descending | Select-Object -First 1
#$ADuser=Get-ADUser -Filter {sAMAccountName -eq $profile.name } -Properties * -server "wca.gov.au" -SearchScope Subtree
$ADcomp=$null
$ADcompPSWD=$null
$ADcomp=get-adcomputer -server "wca.gov.au" -SearchScope Subtree -filter 'Name -like $ThisComputer' -Properties *
$ADcompPSWD=[DateTime]::FromFileTime([Int64] $adcomp.pwdlastset)

if ($ADcomp -ne $null) {write-host $ThisComputer"," $ADcompPSWD"," $ADcomp.LastLogonDate"," $profile.name"," $profile.LastWriteTime}
if ($ADcomp -eq $null) {write-host $ThisComputer", has done gone" }
}

if ($didit -ne $true) {
$ADcomp=$null
$ADcomp=get-adcomputer -server "name.gov.au" -SearchScope Subtree -filter 'Name -like $ThisComputer' -Properties *
write-host $ThisComputer"," "Last Logon" $ADcomp.LastLogonDate, "Not responding"
}
}

No comments:

Blog Archive