Test for Group Membership - Do Something

I need to test if users are member of a security group and if they are not do something.

I have the test part done, but I am trying to get the User Name and LastLogonDate to export to a CSV file.  The Write-Host are there for testing..

$Users = get-aduser -Filter * -searchbase $InactiveUsersOU -Properties * | Where {$_.Enabled -eq $True} | Where {$_.LastLogonDate -lt $(Get-Date).AddDays(-90)}
ForEach ($User in $Users)
{
 If ($User.MemberOf -contains $CN)
 	{ 
	Write-Host "Member, Skip"
	Write-Host $User.Name
	}
	Else
	{
	Write-Host "Not a Member"
	Write-Host $User.Name, $User.LastLogonDate
	select $User.Name, $User.LastLogonDate | Sort User.LastLogonDate | Export-Csv $ReportPath\$FileDate"InactiveUsers.csv" -NoTypeInformation
	}
}


My brain if full, its after lunch, and that led me here....

June 23rd, 2015 12:45pm

This should be easier . "Select object does not work the way you were guessing.

Always read CmdLet help before posting.

get-aduser -Filter "Enabled -eq $true" -searchbase $InactiveUsersOU -Properties * |
	Where-Object { $_.LastLogonDate -lt $(Get-Date).AddDays(-90) -and $_.MemberOf -contains $CN } |
	ForEach-Object{ Write-Host $_.Name, $_.LastLogonDate }
	Select-Object Name, LastLogonDate

Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 1:46pm

Thanks, sometimes I stare and stare and it doesn't seem clear.  I looked back at a few of my earlier attempts and they were on the track as to what you have, but I went off on a crazy tangent somewhere.

Thanks!

June 23rd, 2015 2:13pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics