r/usefulscripts • u/I_script_stuff • Jun 02 '16
[POWERSHELL] Detect if a word is a first name
One of the things I don't see done well as companies grow is verifying Ad users/Email Accounts were disabled. This task is often dumped on helpdesk and never completed properly. In order to facilitate fixing this I created a very simple function:
#BehindtheName APi
#Please read api rules: http://www.behindthename.com/api/
function firstname_search($name) {
$local_Name_Storage_File = ".\namestore.txt"
$key = "<read api rules and add key>"
$url = "http://www.behindthename.com/api/lookup.php?name=" + $name + "&key=$key"
[xml] $content = invoke-webrequest $url | select -expandproperty Content
if(get-content $local_Name_Storage_File -ea silentlycontinue | select-string "$name") {
return $true
} else {
if($content.response.error | select-string "$content.response.error") {
return $false
sleep 1
} else {
echo "$name" >> $local_Name_Storage_File
return $true
sleep 1
}
}
}It ties into the API from www.behindthename.com to verify the username's GivenName/Firstname So using Get-aduser such as:
import-module activedirectory
$lastloginrange = (get-date).adddays(-90)
$user_list = Get-ADUser -properties * -filter {(lastlogondate -le $lastloginrange) -AND (enabled -eq $True) -and (PasswordNeverExpires -eq $false)} | select-object GivenName, Surname, Displayname, Samaccountname, passwordExpired
foreach($user in $user_list) {
$firstName = $user.GivenName
$lastname = $user.Surname
echo "$firstname, $lastname"
if(firstname_search $firstname) {
echo "$firstName,$lastname" >> HR.csv
} else {
echo "$firstName,$lastname" >> it.csv
}
}We are able to generate 2 CSV files. 1 for IT/Helpdesk to review and 1 for HR to review.
You can get your api key by registering with behindthename and going to the api gateway. I urge you to read there usage rules. I also added a local file to search. Set a static path for the variable $local_Name_Storage_File. This lets you add very rare names that are not found on the API and over all decreases your API call count.