在域 AD 中搜索用户

Search for user in domain AD

正在处理这段代码以创建新的用户脚本,但我对 if 语句没有太多经验。我正在尝试让系统检测新用户是否与现有用户匹配,如果匹配,请询问中间名。

$firstName = Read-Host -Prompt 'Please enter new users first name'

$lastName = Read-Host -Prompt 'Please enter new users last name'

$userName = "$firstName.$lastName"

if ($username -match -ADUser)
    {

    write-host "$userName is already in use"
    $Selection = read-host "Would you like to add a middle name? (Y/N)"
    switch ($Selection) {
    'Y'{
    
    $middleName = read-host - prompt "Please enter the users middle initial"
    $userName = "$firstName.$middleName.$lastName"
    
    }
    'N' {
    
    return
    
    }
    }
}
else{

}

我想通了。如果其他人遇到同样的问题,请按照此操作。创建一个用于在 AD 中搜索名称的变量,然后将其与您正在创建的用户进行匹配。

我的代码:

$firstName = Read-Host -Prompt 'Please enter new users first name'
$lastName = Read-Host -Prompt 'Please enter new users last name'

$userName = "$firstName.$lastName"
$activeSearch = Get-ADUser -Identity $userName

if ($activeSearch -match $userName)
    {

        write-host "$userName is already in use"
        $Selection = read-host "Would you like to add a middle name? (Y/N)"
        switch ($Selection) {
        'Y'{
    
            $middleName = read-host - prompt "Please enter the users middle initial"

            $userName = "$firstName.$middleName.$lastName"
    
        }
        'N'{
    
            return
    
        }
    }
}
else{

}