[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Narrow down OU's being audited in PS script

Posted on 2017-12-07
3
Medium Priority
?
31 Views
Last Modified: 2017-12-08
Hey Experts, in the script below, everything is working but the scope of the OU's being audited is too wide.  I need to specify the highest level OU that should be audited.

Example:
DC=Domain
OU=State
-OU=City1
--OU=Agency1
---OU=PC's
---OU=Users
--OU=Agency2
---OU=PC's
---OU=Users

City2
--Agency1
--Agency2

I need to limit the user auditing to just the agency levels to the OU's under City1.  I ran the script and just went through the state level users accounts.  Exciting times.  

Import-Module ActiveDirectory

$disabledOU = "OU=InactiveUsers,DC=my,DC=domain,DC=com"

$users = Get-ADUser -Filter * -Properties SAMAccountName, lastLogonTimeStamp | ?{[DateTime]::FromFileTime($_.lastLogonTimeStamp) -lt (Get-Date).AddDays(-49)}
foreach ($user in $users)
{
    Disable-ADAccount -Identity $user.SAMAccountName -WhatIf
    Move-ADObject -Identity $user.SAMAccountName -TargetPath $disabledOU -WhatIf
}

Open in new window

0
Comment
Question by:samiam41
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
Pber earned 2000 total points
Add a Searchbase to your query to point to the target ou

$users = Get-ADUser -SearchBase "OU=City1,OU=State,DC=my,DC=domain,DC=com" -Filter * -Properties SAMAccountName, lastLogonTimeStamp | ?{[DateTime]::FromFileTime($_.lastLogonTimeStamp) -lt (Get-Date).AddDays(-49)}

Open in new window


This should only find users within the City1 OU and below.
0
 
LVL 9

Author Closing Comment

by:samiam41
Brilliant!!
0
 
LVL 9

Author Comment

by:samiam41
If you can help with my new question, I would appreciate it-> https://www.experts-exchange.com/questions/29072614/Stale-computer-script-PS.html#a42396563
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Join & Write a Comment

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

868 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