本文介绍了Powershell函数生成samAccountName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写PS函数,该函数将 FirstName LastName 作为参数并返回 samaccountname .这是该函数应如何工作的算法.

Im trying to write PS function which is taking FirstName and LastName as parameters and returns samaccountname. Here is the algorithm how the function should work.

现在看看是否使用登录 richard .如果使用 richard ,请尝试 richard.t .如果使用 richard.t ,请尝试 richard.te .如果使用 richard.te ,请尝试 richard.tes ,依此类推.

Now look if login richard is taken. If richard is taken try richard.t. If richard.t is taken try richard.te. If richard.te is taken try richard.tes, and so on.

下面是我的代码.我认为这比使用if循环要容易得多.

Below is my code. I think this can be done in a much easier way than with if loops.

$FirstName = "Richard"
$LastName = "Testing"

function Remove-StringLatinCharacters {
    Param([string]$String)
    [Text.Encoding]::ASCII.GetString([Text.Encoding]::GetEncoding("Cyrillic").GetBytes($String))
}

$FirstnameToUPN = (Remove-StringLatinCharacters -String $firstname).ToLower()
$LastnamenameToUPN = (Remove-StringLatinCharacters -String $lastname).ToLower()

$user = $(try {Get-ADUser $FirstnameToUPN} catch {$null})
if ($user -ne $null) {
    $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]
    $user = $(try {Get-ADUser $upn} catch {$null})
    if ($user -ne $null) {
        $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]
        $user = $(try {Get-ADUser $upn} catch {$null})
        if ($user -ne $null) {
            $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]+$LastnamenameToUPN[2]
            $user = $(try {Get-ADUser $upn} catch {$null})
            if ($user -ne $null) {
                $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]+$LastnamenameToUPN[2]+$LastnamenameToUPN[3]
            } else {
                $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]+$LastnamenameToUPN[2]+$LastnamenameToUPN[3]
            }
        } else {
            $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]
        }
    } else {
        $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]
    }
} else {
    $upn=$FirstnameToUPN
}


编辑#1:

$upn = $FirstnameToUPN
if (Get-Aduser -Filter {SamAccountName -eq $upn}) {
    $upn = $FirstnameToUPN+"."+$LastnamenameToUPN[0]
    if (Get-Aduser -Filter {SamAccountName -eq $upn}) {
        $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]
        if (Get-Aduser -Filter {SamAccountName -eq $upn}) {
            $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]+$LastnamenameToUPN[2]
            if (Get-Aduser -Filter {SamAccountName -eq $upn}) {
                $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]+$LastnamenameToUPN[2]+$LastnamenameToUPN[3]
                if (Get-Aduser -Filter {SamAccountName -eq $upn}) {
                    $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]+$LastnamenameToUPN[2]+$LastnamenameToUPN[3]+$LastnamenameToUPN[4]
                    if (Get-Aduser -Filter {SamAccountName -eq $upn}) {
                        $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]+$LastnamenameToUPN[2]+$LastnamenameToUPN[3]+$LastnamenameToUPN[4]+$LastnamenameToUPN[5]
                    } else {
                        $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]+$LastnamenameToUPN[2]+$LastnamenameToUPN[3]+$LastnamenameToUPN[4]
                    }
                } else {
                    $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]+$LastnamenameToUPN[2]+$LastnamenameToUPN[3]
                }
            } else {
                $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]+$LastnamenameToUPN[2]+$LastnamenameToUPN[3]
            }
        } else {
            $upn = $FirstnameToUPN + "." + $LastnamenameToUPN[0]+$LastnamenameToUPN[1]
        }
    } else {
        $upn = $FirstnameToUPN+"."+$LastnamenameToUPN[0]
    }
} else {
    $upn = $FirstnameToUPN
}

推荐答案

您仅从名字开始,然后附加姓氏中的字符,直到结果帐户名在AD中不存在或字符用完:

You start with just the first name, then append characters from the last name until the resulting account name does not exists in AD or you run out of characters:

$acct = $FirstnameToUPN
$cnt  = 1
while (-not (Get-ADUser -Filter "SamAccountName -eq '$acct'") -and $cnt -le $LastnameToUPN.Length) {
    $acct = "${FirstnameToUPN}." + $LastnameToUPN.Substring(0, $cnt)
    $cnt++
}

if (Get-ADUser -Filter "SamAccountName -eq '$acct'") {
    Write-Error "No unused account name found for ${firstname} ${lastname}."
}

这篇关于Powershell函数生成samAccountName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 14:59