Read AD User Description field & filter based on text information

Hello

I am trying to filter a list of users based on their contents of the description field.

Return only users that don't have the word Enabled & a date of greater than the last 30 days in the description field.

This is what I have so far & is working in that it finds all users that don't have the word enabled in the field.

$30D = (get-date) - (new-timespan -Days 30)

foreach ($u in $users) { Get-QADUser $u -IncludedProperties SamAccountName | Where-Object {($_.Description -notmatch "Enabled")} | Select-Object SamAccountName,Description }

What I need to to do is read the date that is in the description field & if it greater than the 30days return the results.

Thank you in advance.

Steve

April 20th, 2015 9:36am

Hi Steve,

You can use an if test:

$date = '3/10/2015'

If ( ((Get-Date) - (Get-Date $date)).Days -gt 30 ) {

    Write-Output 'More than 30 days has elapsed'

} Else {

    Write-Output '30 days or less'

}

You haven't told us what the description field looks like, so it'll be up to you to get that value.

Free Windows Admin Tool Kit Click here and download it now
April 20th, 2015 9:43am

Hi Mike

Thanks for the response.

The description field will have various data in but here is an example of the one that I would want to exclude from the returned results   "Enabled by :helpdesk on 14/04/2015 09:04:12" by filtering on the word enabled and the date.

Thank You

Steve

April 20th, 2015 10:28am

You'll need to find a consistent way to parse that.

In that particular example I'd probably do something like this:

$str = 'Enabled by :helpdesk on 14/04/2015 09:04:12'

$date = ($str -split ' ')[-2]

If ( ((Get-Date) - (Get-Date $date)).Days -gt 30 ) {

    Write-Output 'More than 30 days has elapsed'

} Else {

    Write-Output '30 days or less'

}

The above will only work if the date is the second to last position in the string.

Free Windows Admin Tool Kit Click here and download it now
April 20th, 2015 10:40am

Thanks Mike

April 21st, 2015 8:36am

Cheers Steve, you're very welcome.
Free Windows Admin Tool Kit Click here and download it now
April 21st, 2015 10:46am

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

Other recent topics Other recent topics