下面是我正在努力获得操作的一段代码。这是创建AD用户的较大脚本的一部分。目的是验证提供的电子邮件地址是否存在,如果存在,则将其存储为创建AD帐户时要调用的$UserManager(Manager(变量。
我觉得我真的很接近,我想我只是在为函数的第一部分或搜索的初始查询而挣扎。我需要指定一个特定的路径吗?
感谢您的帮助,这个论坛让我做了一些了不起的事情。再次非常感谢大家。
此脚本的基本功能值得称赞-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
运行该命令时,它会提示我输入电子邮件地址。然后它立即告诉我;找不到经理的电子邮件地址,请检查电子邮件并重试";。它似乎甚至没有搜索提供的电子邮件地址。
有什么想法吗?
我没有看到任何关于当前代码可能失败的具体指示,但有些地方你应该纠正。您不应该从ManagerCheck
函数中设置$global:
变量,这在我看来是一种特别糟糕的做法,应该尽可能避免,而是应该让您的函数为经理的电子邮件接受一个参数,这样,如果找不到AD对象,并且您输入了if
条件,则可以将该新地址传递给函数的递归调用。除此之外,还不清楚$EmployeeOU
的用途,我没有看到它被使用,所以我决定删除它
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