[AD] Find sub OU’s created inside a specific time frame

Hi there! I’m currently working on a script that will write out some information to an html-file. I’ve nailed the users part, however, I’m struggling with the organizational units and was wondering if any of you would take the time and help me out.

What it does it find the users created in the last month, right now that’s August, and the one prior to that which is July, then it crunches some numbers and makes an output where it shows this Augusts new AD users and July’s new ones.

I’d like for the same to happen with the OU’s inside a specific OU, but I’m ending up with this error message and I’m not the most talented scripter:

Method invocation failed because [Microsoft.ActiveDirectory.Management.ADPropertyValueCollection] does not contain a method named 'op_Subtraction'. At line:54 char:1 + $totalou = [math]::Round(($ousmade - $ousmade2) * 100 / $ousmade,2) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (op_Subtraction:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound 

Here’s my code:

#This function gathers the amount of days in the previous month. This provides an exact way to extract data. function FindDate($months) { $PriorMonth = (Get-Date).AddMonths(-($months)) $FirstDay = 1 $LastDay = [System.DateTime]::DaysInMonth($PriorMonth.Year, $PriorMonth.Month) $end = [datetime]"$($PriorMonth.Month), $LastDay, $($PriorMonth.Year)" $start = [datetime] "$($PriorMonth.Month), $FirstDay, $($PriorMonth.Year)" #Finds users created within given timeframe $users = Get-ADUser -Filter * -Properties whenCreated | ? { ($_.whenCreated -gt $Start) -and ($_.whenCreated -le $End) } $users } #This function gathers how many OU's were created. function FindDateOUsCreated($months) { $PriorMonth = (Get-Date).AddMonths(-($months)) $FirstDay = 1 $LastDay = [System.DateTime]::DaysInMonth($PriorMonth.Year, $PriorMonth.Month) $end = [datetime]"$($PriorMonth.Month), $LastDay, $($PriorMonth.Year)" $start = [datetime] "$($PriorMonth.Month), $FirstDay, $($PriorMonth.Year)" #Finds OUs created within given timeframe $ou = 'OU=test,OU=testst,DC=domain,DC=com' $ous = Get-ADOrganizationalUnit -SearchBase $OU -SearchScope Subtree -Filter * -Properties whenCreated | ? { ($_.whenCreated -gt $Start) -and ($_.whenCreated -le $End) } $ous } #Users created the last calendar month $lastmonth = FindDate 1 $usersmade = $lastmonth.Count $usersmade $OU's created 1 month ago $lastmonthou = FindDateOUsCreated 1 $ousmade = $lastmonthou.Count $ousmade Sleep -Seconds 2 #Users created two months ago $twomonthsago = FindDate 2 $usersmade2 = $twomonthsago.Count $usersmade2 #OU's created 2 months ago $outwo = FindDateOUsCreated 2 $ousmade2 = $outwo.Count $ousmade2 $total = [math]::Round(($usersmade - $usersmade2) * 100 / $usersmade,2) $totalou = [math]::Round(($ousmade - $ousmade2) * 100 / $ousmade,2) 

I am also aware that there’s a lot of ways to look this code more elegant. Could most likely get everything in the same function but you know, not quite there yet. 🙂

submitted by /u/powermasc
[link] [comments]

Leave a Reply