Ada*_*dan 5 powershell active-directory
预先感谢您的帮助。我在以下链接中找到了相关内容,但无法通过此处找到我正在寻找的解决方案。使用 Powershell 脚本检查域帐户的存在
我想要做的是从我正在运行的另一个脚本中已生成到 CSV 文件的列表中获取用户名。
获得这些用户名 (sAMAccountname) 后,我想检查该用户名是否已被使用。如果是,我希望它显示出来,也许通过 Echo,然后继续检查其他名称。如果不是,那么它应该只是继续检查其他名称。
这是我目前所拥有的。(请记住,我是一个完整的 Powershell 新手,我只是出于绝对需要才这样做)
Import-Module ActiveDirectory -ErrorAction Continue
$UserCSV = import-csv "\\fs1\DisasterRecovery\Source Controlled Items\WindowsPowerShell\Test scripts\Import Production Users\Users.csv"
$UserList = $UserCSV.sAMAccountname
foreach($User in $UserList)
{
if (Get-ADUser -Filter {sAMAccountname -eq $User} ) {
# Exists
#echo Already Exists
} else {
SilentlyContinue
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 Powershell 3 或更高版本,则不需要Import-Module ActiveDirectory
. 只要您使用该模块中的 cmdlet,PS 就会自动为您加载该模块。过去$PSVersionTable
肯定知道,但我认为您使用的是 PS 3 或更好的版本,因为您似乎在代码中使用了自动 foreach,而自动 foreach 直到 PS 3 才可用。
此外,如果模块无法加载,则继续操作没有意义,因为它对脚本的其余部分至关重要,因此-ErrorAction Continue
也没有意义。我会打击整个第一行。
导入 CSV 的第二行没问题。该$UserList
变量似乎是多余的。从那里我可能会做这样的事情:
$UserCSV = Import-Csv C:\Users\Administrator\test.csv
Foreach ($User in $UserCSV)
{
Try
{
# Use "EA Stop" to ensure the exception is caught.
$U = Get-ADUser $User.sAMAccountName -ErrorAction Stop
Write-Host "$($User.SamAccountName) is already in use."
}
Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
# The user was not found!
Write-Warning "$($User.SamAccountName) was not found in this domain!"
}
Catch
{
# Some other terrible error occured!
Write-Error "OHSHI"
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15089 次 |
最近记录: |