Functions/Get-DaysSinceLastLogon.ps1
|
function Get-DaysSinceLastLogon { <# .Notes AUTHOR: Skyler Hart CREATED: 02/01/2018 10:31:35 LASTEDIT: 02/01/2018 10:31:35 KEYWORDS: REQUIRES: -RunAsAdministrator .LINK https://wanderingstag.github.io #> [CmdletBinding()] Param ( [Parameter( Mandatory=$false, Position=0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true )] [Alias('User','SamAccountName','Computer','ComputerName','Username')] [string[]]$Name = "$env:USERNAME" ) Begin { $sd = Get-Date } Process { foreach ($obj in $Name) { try {$record = Get-ADUser $obj -Properties LastLogonDate} catch { $nobj = $obj + "$" $record = Get-ADComputer $nobj -Properties LastLogonDate } $name = $record.Name $LLD = $record.LastLogonDate $sam = $record.SamAccountName try { $dsll = [math]::Round((-(New-TimeSpan -Start $sd -End $LLD)).TotalDays) } catch { $dsll = "NA" } [PSCustomObject]@{ Name = $obj DaysSinceLastLogon = $dsll SamAccountName = $sam }#new object } } End {} } |