jueves, 28 de mayo de 2020

Listado de usuarios y última fecha de conexión PowerShell

1. Crear un archivo de texto con el nombre UserLastLogon.ps1

2. Editar con notepad
3. Copiar y pegar el siguiente código:
$NumDays = 0
$LogDir = ".\Users-Last-Logon.csv"

$currentDate = [System.DateTime]::Now
$currentDateUtc = $currentDate.ToUniversalTime()
$lltstamplimit = $currentDateUtc.AddDays(- $NumDays)
$lltIntLimit = $lltstampLimit.ToFileTime()
$adobjroot = [adsi]''
$objstalesearcher = New-Object System.DirectoryServices.DirectorySearcher($adobjroot)
$objstalesearcher.filter = "(&(objectCategory=person)(objectClass=user)(lastLogonTimeStamp<=" + $lltIntLimit + "))"

$users = $objstalesearcher.findall() | select `
@{e={$_.properties.cn};n='Display Name'},`
@{e={$_.properties.samaccountname};n='Username'},`
@{e={[datetime]::FromFileTimeUtc([int64]$_.properties.lastlogontimestamp[0])};n='Last Logon'},`
@{e={[string]$adspath=$_.properties.adspath;$account=[ADSI]$adspath;$account.psbase.invokeget('AccountDisabled')};n='Account Is Disabled'}

$users | Export-CSV -NoType $LogDir

4. Guardar el archivo
5. Ejecutar con powerShel y nos dará un archivo CSV con el resultado


Un saludo

No hay comentarios:

Publicar un comentario

 No funciona la SOMBRA en RDP Habilitar esta regla: En Firewall de Entrada, habilitar Escritorio Remoto - Instantanea (TCP de Entrada)