经理通过Powershell和Get-Aduser进行检查
下面是我正在努力操作的一段代码。这是用于创建 AD 用户的较大脚本的一部分。目的是验证提供的电子邮件地址是否存在,如果存在,则将其存储为创建 AD 帐户时要调用的 $UserManager(管理员)变量。
我觉得我已经很接近了,我想我只是在函数的第一部分或搜索的初始查询中挣扎。我需要指定具体路径吗?
感谢您的帮助,这个论坛让我做了一些了不起的事情。再次非常感谢大家。
感谢此脚本的基本功能 - 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
运行命令时提示我输入电子邮件地址。然后它立即告诉我“找不到经理电子邮件地址,请检查电子邮件并重试”。看起来它甚至没有搜索提供的电子邮件地址。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有看到有关您当前代码为什么会失败的任何特定迹象,但是您应该纠正一些要点。而不是从您的
ManagerCheck
函数设置$ global:
变量,这在我看来尤其是一个不好的做法请参阅管理器的电子邮件的论点,以防万一找不到广告对象并且输入该条件,则可以将新地址传递给函数的递归调用。除此之外,尚不清楚$雇员
是什么,我没有看到它被使用,因此我决定将其删除。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 yourManagerCheck
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 thatif
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.