My first hit of crack, I mean Powershell.

The other day last week, my co-worker and I were guestimating how many users we have in our organization. We figured it was about 1500 users. We decided to Powershell our way to an answer. In case you've been working in a Windowless datacenter for sometime or under a proverbial rock, Powershell is the automation and scripting framework built into all of Microsoft's recent business and enterprise softwares. It can be a shell for single commands like DOS (but so much more) or it can handle complex scripts the same way BASH can. Below, I'll run you through the first Powershell script I wrote with the help of my colleague, Steven.


Get-ADOrganizationalUnit -filter * | select-object DistinguishedName | out-file C:\users\andrewh.GLOBAL\Desktop\OU.txt

I should specify that I had already loaded the the Active Directory PSdrive. This command pulled a list of every Distinguished Name of any Organizational Unit in AD and made it into a text document. I used Notepad++ to go through the list and take out the OUs that do not have actual users. Saved it to my desktop and continued on.

$ou=Get-Content C:\users\andrewh.GLOBAL\Desktop\OU.txt 
 $i=0 
 Foreach ($usr in $ou){ 
$andy = Get-ADUser -Filter * -SearchBase $usr -SearchScope onelevel 
 $andy.count 
if ($i=0){$dog=$andy.count} 
else {$dog=$andy.count+$dog} 
$dog }

This bit pulled in the list of Distinguished Name for each OU with users in it, counted the users in each OU, and listed the total of users in each OU. Since I was too lazy to add them up manually, I had Powershell do it for me.

$andrew = Get-ADUser -Filter * -SearchBase "OU=People,OU=Users,OU=Housing,DC=my,DC=work,DC=place" -SearchScope Subtree
$andrew.count


This added the users together and listed the final number at 1564. I've changed some information to protect the identity of my employer obviously. It was quite fun to turn a tedious process into a nifty script that I can run anytime again and get a new total of users later. I consider that automated.

I'm hooked. Like most drugs, the first hit was free. From here on out, it'll only cost me time. ;)

Comments

Popular posts from this blog

Installing CentOS 7 on a Raspberry Pi 3

Modifying the Zebra F-701 & F-402 pens

How to fix DPM Auto-Protection failures of SQL servers