Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Add query for last login time to script

$
0
0

Hello, 

I have a script that works great to report on the local admin group or any group of users for servers.

I need to add in some queries and to start with I have to get the last login time stamp of the "local user" accounts.

I've tried adding in a query used in another script on this site but it fails.  Can anyone assist?

#>

[CmdletBinding()]

Param(

 [Parameter( ValueFromPipeline=$true,

 ValueFromPipelineByPropertyName=$true

 )]

 [string[]]

 $ComputerName = $env:ComputerName,

 

 [Parameter()]

 [string]

 $GroupName = "Administrators",

 

 [Parameter()]

 [string]

 $OutputFolder = "c:\temp\"

)

 

Begin {

 

 $OutputFile = Join-Path $OutputFolder "Adminrpt.CSV"

 Write-Verbose "Script will write the output to $OutputFile folder"

 Add-Content -Path $OutPutFile -Value "ComputerName, GroupName, QueryResult, ObjectType, DomainName, Name"

}

 

Process {

 ForEach($Computer in $ComputerName) {

 Write-host "Working on $Computer"

 If(!(Test-Connection -ComputerName $Computer -Count 1 -Quiet)) {

 Write-Verbose "$Computer is offline. Proceeding with next computer"

 Add-Content -Path $OutputFile -Value "$Computer,$GroupName,Offline"

 Continue

 } else {

 Write-Verbose "Working on $computer"

 try {

 $group = [ADSI]"WinNT://$Computer/$GroupName"

 $members = @($group.Invoke("Members"))

 Write-Verbose "Successfully queries the members of $computer"

 if(!$members) {

 Add-Content -Path $OutputFile -Value "$Computer,$GroupName,NoMembers"

 Write-Verbose "No members found in the group"

 continue

 }

 } 

 catch {

 Write-Verbose "Failed to query the members of $computer"

 Add-Content -Path $OutputFile -Value "$Computer,,QueryFailed"

 Continue

 }

 foreach($member in $members) {

 try {

 $MemberDisplayName = $member.GetType().Invokemember("Name","GetProperty",$null,$member,$null)

 $ObjectType = $member.GetType().Invokemember("Class","GetProperty",$null,$member,$null)

 $MemberPath = $member.GetType().Invokemember("ADSPath","GetProperty",$null,$member,$null)

 $MemberDomain = $null

 if($MemberPath -match "^Winnt\:\/\/(?<domainName>\S+)\/(?<CompName>\S+)\/") {

 if($ObjectType -eq "User") {

 $ObjectType = "LocalUser" 

 } elseif($ObjectTypee -eq "Group"){

 $ObjectType = "LocalGroup"

 }

 $MemberDomain = $matches["CompName"]

 

 } elseif($MemberPath -match "^WinNT\:\/\/(?<domainname>\S+)/") {

 if($ObjectType -eq "User") {

 $ObjectType = "DomainUser"

 } elseif($ObjectType -eq "Group"){

 $ObjectType = "DomainGroup"

 }

 $DomainOfMember = $matches["domainname"]

 

 } else {

 $ObjectType = ""

 $DomainOfMember = "Unknown"

 }

 Add-Content -Path $OutPutFile -Value "$Computer, $GroupName, SUCCESS, $ObjectType, $DomainOfMember, $MemberDisplayName"

 } catch {

 Write-Verbose "failed to query details of a member. Details $_"

 Add-Content -Path $OutputFile -Value "$Computer,,QueryFailed"

 }

 

 } 

 }

 

 }

 

}

End {}


Viewing all articles
Browse latest Browse all 6937

Trending Articles