Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

one-liner getting accounts that are flagged "password never expires"

Posted on 2016-08-09
9
Medium Priority
?
42 Views
Last Modified: 2016-08-09
I'm trying to use Powershell to give me the user accounts of those accounts that have password set to not expire.
I have written this:
Search-ADAccount -PasswordNeverExpires -usersonly |get-aduser -Filter 'name -like "*wildcard*"'  | FT Name, ObjectClass

Open in new window

When run, this first supplies everything that has my *wildcard* in the name, I thought the first part of the script would only send the user accounts that are  set to not expire, clearly it does not.

Second, after the list of users is complete it throws an error over and over for 10-15 seconds:
Search-ADAccount : The server has returned the following error: invalid enumeration context.
At line:1 char:1
+ Search-ADAccount -PasswordNeverExpires -usersonly |get-aduser -Filter 'name -lik ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Search-ADAccount], ADException
    + FullyQualifiedErrorId : The server has returned the following error: invalid enumeration context.,Microsoft.ActiveDirectory.Management.Commands.SearchADAccount
0
Comment
Question by:SquigglyMonkey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:FOX
ID: 41748772
Get-Aduser  -properties * -filter "PasswordNeverExpires -eq 'True'" | ft Samaccountname, DisplayName,PasswordNeverExpires
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41748779
The first part will indeed only return accounts where the password is set to not expire.
The issue is that in the next pipeline step, you're querying again for all users matching the wildcard. What you need instead of Get-ADUser is a simple Where-Clause.
The other error might be caused by the first, because you're pretty much spamming AD with queries.
How many user accounts are we talking about?
Search-ADAccount -PasswordNeverExpires -UsersOnly  -ResultSetSize $Null | ? {$_.Name -like "*wildcard*"}  | FT Name, ObjectClass 

Open in new window

0
 
LVL 1

Author Comment

by:SquigglyMonkey
ID: 41748808
FOXLUV,
That won't work, it just gives me  all the accounts that are set to not expire. I am looking for a subset of those accounts that contain a specific set of characters.
odba,
I see, get-aduser doesn't care what is piped to it, it just looks at AD. I'll try what you sent and see what happens.
There are north of 50k users. thousands of which are legitimately non-expiring. But I ran into a few specific user names that were set to not expire, and should not be. They all contain something that I can filter on.
Thank you.
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 16

Expert Comment

by:FOX
ID: 41748820
Give me what you can filter on and I will attempt to send you the correct command.  Are all the users in the same OU?  We can target the OU if they are
0
 
LVL 1

Author Comment

by:SquigglyMonkey
ID: 41748889
Foxluv, thanks, "priv-" or ADM- is what the accounts start with. Unfortunately, the way AD was setup in the first place, the accounts are in multiple OU's (from  geographical dispersement of sites).

odba, Thanks that is super close, It's usable, just giving me a few extra names. I tried to change -like to -contains, but that does not work at all. I tried to add or to is since the accounts start with a couple of different things.

Thanks again.
0
 
LVL 13

Expert Comment

by:Dustin Saunders
ID: 41748899
Maybe I'm not understanding by why not add your wildcard to the filter?

Get-ADUser -Properties * -Filter {PasswordNeverExpires -eq "True" -and name -like "*wildcard*"}

Open in new window

0
 
LVL 16

Assisted Solution

by:FOX
FOX earned 1000 total points
ID: 41748912
Get-Aduser  -properties * -filter "Samaccountname -like 'priv*'" | sort PasswordNeverExpires | ft Samaccountname, DisplayName,PasswordNeverExpires

The other one would be
Get-Aduser  -properties * -filter "Samaccountname -like 'ADM*'"| sort PasswordNeverExpires |ft Samaccountname, DisplayName,PasswordNeverExpires
0
 
LVL 85

Accepted Solution

by:
oBdA earned 1000 total points
ID: 41748944
$WildCards = @('priv-*', 'adm-*')
Search-ADAccount -PasswordNeverExpires | ? {$Name = $_.Name; $WildCards | ? {$Name -like $_}} | FT Name, ObjectClass

Open in new window

0
 
LVL 1

Author Closing Comment

by:SquigglyMonkey
ID: 41749016
Thank you both for helping out with this.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question