检查具有类似UPN的用户的AD [英] Check AD for users with similar UPN
问题描述
大家好,
我对变量$ existingNames的下面脚本有问题。我无法让其他用户使用相同的UPN(jesty.daniel2 @ domain.com,jesty.daniel3 ....)
I have an issue with the below script for the variable $existingNames. I am unable to get the other users with the same UPN (jesty.daniel2@domain.com,jesty.daniel3....)
如何修剪upn并获取列表用户并获得建议的名称。请协助。
How can i trim the upn and get the list of users and get the proposed name. please assist.
$ User = Get-ADUser -Filter'UserPrincipalName-like" jesty.daniel@domain.com"'
$ existingNames = $ User .UserPrincipalName -join','
$
#获取首选/所需名称和使用中的现有变体
$ desiredName =" jesty.daniel@domain.com" < br $>
$ FirstName ='Jesty'
$ DomainSFX ='@ domain.com'
$
$ desiredUPN = $ desiredName.Replace($ DomainSFX," ;")
#$ existingUPN = $ existingNames.Trim($ DomainSFX)
#解析现有名称并提取正在使用的索引列表,例如1,2,3
$ existingIndexList = $ existingNames -split"," | ForEach-Object {$ _.Trim($ DomainSFX)} | ForEach-Object {$ _ -replace $ desiredUPN,""} | Where-Object {$ _ -match" ^ \d + $"} | ForEach-Object {[int] $ _}
#通过排序确定使用的最高索引
if(!($ existingIndexList)){
$ lastIndex = 1
}
else {
$ lastIndex = $ existingIndexList | Sort-Object | Select-Object -Last 1
}
#增加下一个索引并使用它来创建一个建议的唯一用户名
$ availableIndex = $ lastIndex + 1
$ FirstName = $ FirstName +''+ $ availableIndex
$ proposedName = $ desiredUPN + $ availableIndex
$User = Get-ADUser -Filter 'UserPrincipalName -like "jesty.daniel@domain.com"'
$existingNames = $User.UserPrincipalName -join ','
# Get the preferred/desired name and the existing variants in use
$desiredName = "jesty.daniel@domain.com"
$FirstName = 'Jesty'
$DomainSFX = '@domain.com'
$desiredUPN = $desiredName.Replace($DomainSFX,"")
#$existingUPN = $existingNames.Trim($DomainSFX)
# Parse the existing names and extract a list of indexes in use, e.g. 1,2,3
$existingIndexList = $existingNames -split "," | ForEach-Object {$_.Trim($DomainSFX)} | ForEach-Object {$_ -replace $desiredUPN,""} | Where-Object {$_ -match "^\d+$"} | ForEach-Object {[int]$_}
# Determine the highest index in use by sorting
if (!($existingIndexList)){
$lastIndex = 1
}
else {
$lastIndex = $existingIndexList | Sort-Object | Select-Object -Last 1
}
# Increment to get next index and use this to create a proposed unique username
$availableIndex = $lastIndex + 1
$FirstName = $FirstName +' '+ $availableIndex
$proposedName = $desiredUPN + $availableIndex
Justin
推荐答案
-Like运算符需要"*"通配符。但请查看-Match运算符:
The -Like operator requires the "*" wildcard character. But look into the -Match operator:
这篇关于检查具有类似UPN的用户的AD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!