官术网_书友最值得收藏!

Searching for and reporting on AD users

Once your AD environment has existed for some time, finding and changing settings in your environment can become difficult. For example, let's say when the domain was first created, all the users had the same logon script named logon.bat. Over time, specific needs arose that caused the creation of logon2.bat, and new_logon.bat, and testlogon.bat, with different users assigned to each script.

As an administrator, you want to consolidate all these logon scripts into one, but you need to know what this will impact. You need to know which logon scripts are being used, who is using which ones, and why the different scripts exist. Thanks to the capabilities of AD and PowerShell queries, these items can easily be found.

In this recipe we will perform multiple queries against Active Directory. We will be returning different information.

How to do it...

Carry out the following steps to search for and report on AD users:

  1. To report on all users and their logon scripts execute the following code:
    Get-ADUser -Filter * -Properties SamAccountName, DisplayName, `
    ProfilePath, ScriptPath | `
    Select-Object SamAccountName, DisplayName, ProfilePath, ScriptPath 
  2. To find all disabled user accounts execute the following code:
    Get-ADUser –Filter 'Enabled -eq $false' 
  3. To find users who haven't logged in for 30 days execute the following code:
    $logonDate = (Get-Date).AddDays(-30)
    Get-ADUser -Filter 'LastLogonDate -lt $logonDate' | Select-Object DistinguishedName
  4. To find accounts with multiple logon failures execute the following code:
    $primaryDC = Get-ADDomainController -Discover -Service PrimaryDC
    Get-ADUser -Filter 'badpwdcount -ge 5' -Server $primaryDC.Name `
    -Properties BadPwdCount | Select-Object DistinguishedName, BadPwdCount

How it works...

The first example uses Get-ADUser and queries Active Directory for all User objects, and returns them to PowerShell. The accounts are then piped through Select-Object to return the username, profile location, and logon script to the screen.

The second example creates a simple filter for AD to show accounts that are not enabled. Get-ADUser is called with this filter and it returns the appropriate accounts.

The third example creates a slightly more robust AD filter that identifies users, who have not logged in for more than 30 days. We start by creating the $logonDate variable and assigning it with the date 30 days ago. We then call Get-ADUser with a filter based on the LastLogonDate attribute, and lastly return Distinguished Name of the users that match the filter. These may be users who have been on vacation, extended work trips and have not been able to log on to the domain, or user accounts that no longer are needed.

The fourth example provides a simple filter based on the number of bad password attempts for an account. We start by identifying the primary domain controller (PDC) in the environment. When a bad password attempt occurs, it is tracked on the domain controller initially queried, as well as the PDC emulator. This allows us to query a single server instead of all the DCs. If a particular account has a high number of password failures, it can be a sign of possible hacking attempt. As such, it is always helpful to have a script similar to this available so that the entire domain can be reviewed at a glance to determine if one user is having a problem, or if it is wide spread issue.

主站蜘蛛池模板: 蓬安县| 安庆市| 宾阳县| 长宁县| 清水河县| 广宁县| 台南县| 亳州市| 息烽县| 张家川| 锡林郭勒盟| 陆良县| 海安县| 岳池县| 勐海县| 绥江县| 平南县| 凤冈县| 萨嘎县| 永仁县| 镇原县| 新竹市| 奇台县| 嘉义县| 南溪县| 鱼台县| 铜陵市| 朝阳市| 凤翔县| 抚州市| 安多县| 吴江市| 喀什市| 怀集县| 德清县| 开原市| 绥德县| 临清市| 静乐县| 田林县| 永和县|