Posts

Showing posts from January, 2019

Simple array in PowerShell script

Here is a baseline of nested arrays in PowerShell script: PS: Currently output is not sorted, looking for improvements: <#  JD Script to find Last logon time #> $array = @() foreach ($ID in $IDs) { $User = Get-ADUser -Server $dc -Filter {Name -like $ID} -Properties * $obj = New-Object psobject -Property @{  Name = $User.Name            LastLogonDate = $User.LastLogonDate             } $array += $obj }                       #$array | Select * | FT -AutoSize $array | Export-Csv -path $outfile -NoTypeInformation  

PowerShell Script to Get Last logged-on time of AD users of different domain

Scenario: You need to get Last logged-on time of AD users of different domain. Below are the steps: Step 1 : Find a DC of that domain: Get-ADDomainController -DomainName -Discover -NextClosestSite Above command outputs server name of given domain. Step 2: Get-ADUser properties in that server Get-ADUser -Server -Filter {Name -like " "} | Get-ADObject -Properties lastLogon Possible Filters that can be used: GivenName                : First Name Surname                      : Last name Name                          :Login id SamAccountName      :  Login id UserPrincipalName     : Login id@domain.com My PowerShell script: $GivenDomain = "mydomain.com" $IDs = @ ( "myadaccount1" " myadaccount2 " " myadaccount3 " " myadaccount4 " ) $ErrorActionPreference = "silentlycontinue" $dc = Get-ADDomainController -DomainName $GivenDomain -Discover -NextClosestSite write-host