Categories
Welcome Center
Education & Development
Cyber Security
Virtualization
General
Certification Preparation
Project Management
Posts
Groups
Training Resources
Infosec
IT & Security Bootcamps
Practice Exams
Security Awareness Training
About Us
Home
General
Off-Topic
Breaking up variables in Powershell
mikedisd2
I need a script to query all AD user accounts in OU by the date in the description property.
A Disabled user's AD account description looks like this: "Disabled 13/1/2011." If the description contains a date older than the date one week ago, then take action.
# Retrieve current date and subtract 7x days
$LastWeek = ((Get-Date).AddDays(-7))
$Date = $LastWeek.ToShortDateString()
If ($User.description -contains
*Integer in date format*
-le $date))
{Write-Host "Hell yeah!"}
I guess searching a string is what I'm trying to do. Any ideas?
EDIT: Variable logic above was incorrect; adjusted.
Find more posts tagged with
Comments
sidsanders
in addition to the first few vars you have set, try something like...
if ($User.description.Contains("Disabled")) {
$tempdate = $User.description.TrimStart("Disabled - ")
# may need to replace the . with "" -- or nothing that is...
if ($tempdate -le $Date) {
<do stuff>
}
}
this assumes the description field contains "Disabled - dd/mm/yyyy"
mikedisd2
Here's what I ended up with:
# Retrieve current date and subtract 7x days
$LastWeek = ((Get-Date).AddDays(-7))
$OneWkAgo = $LastWeek.ToShortDateString()
# Get the disabled users'OU
$UserOU = Get-QADUser -SearchRoot Domain.int/Users/Disabled
$UserArr=
@()
$ADUser=@{}
# Store User description and logon name into an array
foreach($ADUser in $UserOU)
{
$UserArr += ,
@($ADUser
.Description, $ADUser.LogonName)
}
# Find account 'descriptions' properties starting with "Disabled Date"
for ($i = 0; $i -lt $UserArr.Length; $i++)
{
$ADUser = $UserArr -like "Disabled Date*"
}
# Disable users with description date >= 7days old.
for ($i = 0; $i -lt $ADUser.Length; $i++)
{
$ADUserRow = $ADUser[$i]
$DisabledDate = $ADUserRow.Substring(14,10)
$UserLogon = $ADUserRow.Substring(25)
if ($DisabledDate -le $OneWkAgo)
{
$UserToDisable = $ADUser[$i]
Disable-QADUser $UserLogon
}
}
I played around with
var
.trimstart until I realised I needed to use arrays and .trimstart wasn't applicable. However it got me started so thanks for the help on this one. I'm sure there are more effective ways of accomplishing this as this seemed a bit cumbersome but it works.
Quick Links
All Categories
Recent Posts
Activity
Unanswered
Groups
Best Of