Mark Minasi's Tech Forum
Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 93
Reply with quote  #1 
The code below prompts the user for an email address.  Based on the email address it gets employeeNumber.  This is a mandatory and therefore both mail and employeeNumber to found in AD.  I need to include error handling so that that both mail ($inputRequestedby) and employeeNumber belong to a user in AD.


# get Requested By - Mandatory

$inputRequestedBy = Read-Host -Prompt "Enter email address of employee who requested this account [Required]"

$inputRequestedByEmployeeNumber = Get-ADUser -Filter {mail -eq $inputRequestedBy} -Properties mail,employeeNumber | Select-Object -ExpandProperty employeeNumber

while (!$inputRequestedBy) {

 Write-Host "ERROR: Requestor's email address is Required."

 $inputRequestedBy = Read-Host -Prompt "Enter email address of employee who requested this account" }

 Write-Host "Input read: $inputRequestedBy ($inputRequestedByEmployeeNumber)" 

0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 371
Reply with quote  #2 
Using code from my reply to the other post, concerning dates, I am sure you can solve this by yourself :-)
__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 93
Reply with quote  #3 
Yes! Your other posted helped! :-)


This is what I came up with:


do {
     $inputRequestedBy = Read-Host -Prompt "Enter email address of employee who requested this account [Required]"
     $inputRequestedByEmployeeNumber = Get-ADUser -Filter {mail -eq $inputRequestedBy} -Properties mail,employeeNumber | Select-Object -ExpandProperty employeeNumber
     if ($inputRequestedByEmployeeNumber) {
     Write-host "$inputRequestedBy | $inputRequestedByEmployeeNumber"}
} until ($inputRequestedBy -and $inputRequestedByEmployeeNumber)


I tried both scenarios where the mail attribute is blank and the employeeNumber is blank and both times it keeps prompting me until a valid mail and employeeNumber are found. 


Thanks (again)!


0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 93
Reply with quote  #4 
I just thought of something --- the code above will search AD for a valid employeeNumber and mail attribute, but it needs to be a valid employeeNumber and mail from the same user account.  
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 93
Reply with quote  #5 
Actually, I think it will check for the same account because the $inputRequestedByEmployeeNumber is based on the mail filter from $inputRequstedBy.
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 93
Reply with quote  #6 
Ahhhhh,

so if I just hit the Enter key without typing anything then I get an error:

     Get-ADUser : The search filter cannot be recognized

0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.