经理通过Powershell和Get-Aduser进行检查

发布于 2025-01-18 11:42:38 字数 1441 浏览 1 评论 0原文

下面是我正在努力操作的一段代码。这是用于创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2025-01-25 11:42:38

我没有看到有关您当前代码为什么会失败的任何特定迹象,但是您应该纠正一些要点。而不是从您的ManagerCheck函数设置$ global:变量,这在我看来尤其是一个不好的做法请参阅管理器的电子邮件的论点,以防万一找不到广告对象并且输入该条件,则可以将新地址传递给函数的递归调用。除此之外,尚不清楚$雇员是什么,我没有看到它被使用,因此我决定将其删除。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文