Reputation: 23
Below is a section of code I'm struggling to get operation. This is part of a larger script to create AD users. The purpose is to verify if the email address supplied exists and if it does, store it as the $UserManager (Manager) variable to be called upon when making the AD account.
I feel like I'm really close, I think I'm just struggling with the first part of the function or the initial query of the search. Do I need to specify a specific path?
Thank you for any assistance, this forum has allowed me to do some amazing things. Thank you all once again so much.
Credit for the base functionality of this script - https://github.com/HanSolo71/Active-Directory-Create-User-and-Mailbox/blob/master/CreateUserFullFunction.ps1
Import-Module ActiveDirectory
function ManagerCheck {
$UserManagerCheck = Get-ADUser -Filter {mail -eq "$UserManager"}
if ($UserManagerCheck = [string]::IsNullOrWhiteSpace($UserManagerCheck))
{
cls
$global:UserManager = (Read-Host -Prompt "Manager email address not found please check the email and try again")
$UserManagerCheck = $null
ManagerCheck
}
else
{
{continue}
CLS
}
}
$UserManager = @()
$UserManagerCheck = @()
$global:UserManager = @()
$EmployeeOU = "OU=Sample,OU=Path"
$UserManager = (Read-Host -Prompt "Please enter the users managers email address")
while ([string]::IsNullOrWhiteSpace($UserManager)) {$UserManager = Read-Host 'You left the email field empty, please input a manager email address'}
#Run manager check function
ManagerCheck
Write-Host
$UserManager
When running the command it prompts me to enter in an email address. It then immediately tells me "Manager email address not found please check the email and try again". It appears that it is not even searching for the supplied email address.
Any ideas?
Upvotes: 1
Views: 961
Reputation: 60110
I'm not seeing any specific indication on why your current code could be failing however there are some points you should correct. Instead of setting a $global:
variable from your ManagerCheck
function, which is particularly a bad practice in my opinion and should be avoided whenever possible, you should make your function take one argument for the manager's email so that, in case the AD Object is not found and you enter that if
condition, then you can pass that new address to the recursive call of the function. Aside from that, it's not clear what $EmployeeOU
is for, I'm not seeing it being used hence I decided to remove it.
Import-Module ActiveDirectory
function ManagerCheck {
[cmdletbinding()]
param(
[parameter(Mandatory)]
[string] $ManagerMailAddress
)
$UserManagerCheck = Get-ADUser -Filter "mail -eq '$ManagerMailAddress'"
if (-not $UserManagerCheck) {
Clear-Host
$tryAgain = Read-Host "Manager email address not found please check the email and try again"
ManagerCheck -ManagerMailAddress $tryAgain
}
else {
# return the ad object of the manager?
$UserManagerCheck.SamAccountName
}
}
$UserManager = Read-Host "Please enter the users managers email address"
while ([string]::IsNullOrWhiteSpace($UserManager)) {
$UserManager = Read-Host 'You left the email field empty, please input a manager email address'
}
#Run manager check function
ManagerCheck -ManagerMailAddress $UserManager
Upvotes: 1