【问题标题】:Check a username list and if they already exist Powershell检查用户名列表,如果它们已经存在 Powershell
【发布时间】:2019-11-07 08:57:59
【问题描述】:
我有一个包含 100 个用户名的 CSV,我现在必须检查它们是否已经存在。最好的方法是什么?
如果用户名“marmar”已被使用,程序是否有可能自行检查用户名“marmar1”是否也被使用,或者“marmar2”是否免费?
通过 csv 读取用户名更容易还是应该将它们复制到 Powershell 中?
用户名示例:
马尔马尔
兰加斯
呸呸呸
低矮的
贝雷帽
法律爪1
等等
欢迎提出想法和小费。$
非常感谢
【问题讨论】:
标签:
powershell
csv
active-directory
【解决方案1】:
如果您的 CSV 文件看起来像
"UserName","OtherStuff"
"marmar","blah"
"langas2","blahblah"
"ianmow","blahblahblah"
"lowbob","blahblahblahblah"
"berret","blahblahblahblahblah"
"lawpaw1","blahblahblahblahblahblah"
你可以这样做:
$freeUserNames = Import-csv 'D:\UsersNamesToCheck.csv' | ForEach-Object {
#Check to see if the user already exists in AD
$name = $_.UserName
$dupes = Get-ADUser -Filter "SamAccountName -like '$name*'" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty SamAccountName
$index = 1
# check if the username is already in use and if so, increase the index number and test again
while ($dupes -contains $name) {
$name = '{0}{1}' -f ($name -replace '\d+$'), $index++
}
$name
}
# output on screen
Write-Host "Unused usernames:`r`n"
$freeUserNames
可能的输出:
Unused usernames:
marmar
langas1
ianmow1
lowbob
berret1
lawpaw2