【问题标题】:AD samAccountname scripting errorAD samAccountname 脚本错误
【发布时间】:2017-07-03 16:48:35
【问题描述】:

我正在尝试输入一个 csv 文件,其中包含用户的名字和姓氏信息。当我运行下面列出的命令时,samAccountName 没有给我预期的输出。

请看下面,让我知道应该更正什么。

输入:

GivenName,LastName,Password,TargetOU,Description,Manager
Jeffrey,Terry,Pass12,"OU=Users,DC=mmc,DC=local",mmc user,knadella
A,King,Pass13,"OU=Users,DC=mmc,DC=local",mmc user,knadella
Chris ,Charles,Pass14,"OU=Users,DC=mmc,DC=local",mmc user,knadella

命令:

$samAccountName = ($csvcontent.GivenName.Substring(0,1))+( $csvcontent.LastName)

当前输出:

J A C Terry King Charles

期望的输出:

ATerry, AKing and CCharles 

请帮忙。谢谢!!

【问题讨论】:

  • J A C 从哪里来?你能展示几个 csv 行的例子吗?
  • GivenName LastName Password TargetOU 描述 Manager Jeffrey Terry Pass12 OU=Users,DC=mmc,DC=local mmc user knadella A King Pass13 OU=Users,DC=mmc,DC=local mmc user knadella Chris Charles Pass14 OU=Users,DC=mmc,DC=local mmc user knadella
  • 有什么想法@Avshalom 吗?
  • 添加了答案,祝你好运
  • 我有相关的问题,虽然我是初学者。 ....如果有子域(childa,abc.com childb.abc.com,childc.abc.com)并且包含相同的samaccountname(123456789)AD将如何进行身份验证?是否会一一检查子域以便对用户进行身份验证?我的应用程序仅使用 samaccountname 作为登录 ID。每个子域中这个 samaccount 的密码是什么不同... ??

标签: powershell active-directory


【解决方案1】:

您正在一次汇总所有详细信息,将GivenNamecolumn (J A C) 的结果与LastName 列 (Terry King Charles) 的结果相结合`

循环遍历每个用户:

foreach($user in $csvcontent){
    [array]$samAccountName += $user.GivenName[0] + $user.LastName
}

输出:

JTerry AKing CCharles

【讨论】:

  • 谢谢@gms0ulman !!这就是我所做的。 1. $csvcontent = Import-CSV -Path "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\import_create_ad_users_2a.csv" 2. $samAccountName += $user.GivenName[0] + $user.LastName 3 . $samAccountName PS C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell> $samAccountName o/p 只给我名字而不是 firstnameinitial+lastname Jeffrey A Baker
  • @Karthik 您是否使用了foreach,并转换为[array]?我在您的代码中没有看到它。在 Excel 或类似文件中打开 CSV 文件并确认其格式正确,列中的值应为。
【解决方案2】:

会给你我每天使用大约 30 次的东西。您希望创建它的方式会破坏一些登录选项。

# <FirstLetterGivingName><LastName> for example
# WGates (William Gates)
 $sam = $_.GivenName.substring(0,1)+$_.Lastname

这是一件好事,但随着公司的发展,您将开始遇到用户名相同的问题。这不是一个完美的解决方案,但使用 GivenName (0,3) 会给您前三个字母。这通常会解决这个问题。如果你打了一个前三个字母和姓氏相同的人,这真的很少见,但它可能会发生。

$sam = $_.GivenName.substring(0,3)+$_.Lastname

我也看到公司这样做,但建议不要这样做,因为用户很难记住登录信息。

$sam = $_.GivenName.substring(0,1)+$_.Lastname.substring(0,7)

这个脚本已经被使用了数千次,但为了这篇文章做了一点修改。

#Test to make sure your output looks correct
#You can do this by running the following:
#Import-csv ".\import_create_ad_users.csv" | Out-GridView

# ERROR REPORTING ALL
Set-StrictMode -Version latest
Import-Module ActiveDirectory

#----------------------------------------------------------
#STATIC VARIABLES
#----------------------------------------------------------

$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$newpath = $path + ".\import_create_ad_users.csv"
$log = $path + ".\create_ad_users.log"
$date = Get-Date
$i = 1
#$addn = (Get-ADDomain).DistinguishedName
#$dnsroot = (Get-ADDomain).DNSRoot
$DNdom = Get-ChildItem -Path Ad:\ | where {$_.Name -eq "Configuration"}            
$addn = ($DNdom.DistinguishedName -split "," ,2)[1] 
$wmiDomain = Get-WmiObject Win32_NTDomain -Filter "DnsForestName = '$( (Get-WmiObject Win32_ComputerSystem).Domain)'"
$dnsroot = $wmiDomain.DomainName  + ".local"

#----------------------------------------------------------
#START FUNCTIONS
#----------------------------------------------------------
Function Start-Commands
{
  Create-Users
}

Function Create-Users
{
  "Processing started (on " + $date + "): " | Out-File $log -append
  "--------------------------------------------" | Out-File $log -append
  Import-CSV $newpath | ForEach-Object {
      If (($_.GivenName -eq "") -Or ($_.LastName -eq ""))
      {
        Write-Host "[ERROR]`t Please provide valid GivenName and LastName. Processing skipped for line $($i)`r`n"
        "[ERROR]`t Please provide valid GivenName and LastName. Processing skipped for line $($i)`r`n" | Out-File $log -append
      }
      Else
       {

        # Replace dots / points (.) in names, because AD will error when a 
        # name ends with a dot (and it looks cleaner as well)
        $replace = $_.Lastname.Replace(".","")
        If($replace.length -lt 4)
            {
            $lastname = $replace
            }
        Else
            {
            $lastname = $replace.substring(0,4)
            }
        # Create sAMAccountName according to this 'naming convention':
        # <FirstLetterInitialGivingName><LastName> for example
        # WGates (William Gates)
        $sam = $_.GivenName.substring(0,1)+$_.Lastname
        Try   { $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$sam)" }
        Catch { }
        If(!$exists)
            {
          # Set all variables according to the table names in the Excel 
          # sheet /import CSV. The names can differ in every project, but 
          # if the names change, make sure to change it below as well.
          $setpass = ConvertTo-SecureString -AsPlainText $_.Password -force

          Try
          {
            Write-Host "[INFORMATION]`t User is now being built : $($sam)"
            "[INFORMATION]`t User is now being built : $($sam)" | Out-File $log -append
            New-ADUser $sam -path $_.TargetOU -GivenName $_.GivenName -Initials $_.Initials `
            -Surname $_.LastName -UserPrincipalName ($sam + "@" + $dnsroot) -DisplayName ($_.GivenName + " " + $_.LastName) `
            -Description $_.Description -Manager $_.Manager -AccountPassword $setpass -Enabled $TRUE -ChangePasswordAtLogon $TRUE
            Write-Host "[INFORMATION]`t Created a new user named : $($sam)"
            "[INFORMATION]`t Created new user named: $($sam)" | Out-File $log -append

             $dn = (Get-ADUser $sam).DistinguishedName

            # Rename the object to a good looking name
            $newdn = (Get-ADUser $sam).DistinguishedName
            Rename-ADObject -Identity $newdn -NewName ($_.GivenName + " " + $_.LastName)
            Write-Host "[INFORMATION]`t Renamed the user $($sam) to $($_.GivenName) $($_.LastName)`r`n"
            "[INFORMATION]`t Renamed the user $($sam) to $($_.GivenName) $($_.LastName)`r`n" | Out-File $log -append

          }
          Catch
          {
            Write-Host "[ERROR]`t Oops, something went wrong: $($_.Exception.Message)`r`n"
          }
        }
        Else
        {
          Write-Host "[SKIP]`t User $($sam) ($($_.GivenName) $($_.LastName)) already exists or returned an error!`r`n"
          "[SKIP]`t User $($sam) ($($_.GivenName) $($_.LastName)) already exists or returned an error!" | Out-File $log -append
        }
      }
    Else
    {
      Write-Host "[SKIP]`t User ($($_.GivenName) $($_.LastName)) will be skipped for processing!`r`n"
      "[SKIP]`t User ($($_.GivenName) $($_.LastName)) will be skipped for processing!" | Out-File $log -append
    }
    $i++
  }
  "Processing ended (on " + $date + "): " | Out-File $log -append
  "--------------------------------------------" + "`r`n" | Out-File $log -append
}


Write-Host "***************************SCRIPT HAS STARTED***************************"
Write-Host "***************************SCRIPT HAS STARTED***************************"
Write-Host "***************************SCRIPT HAS STARTED***************************`r`n"
Start-Commands
Write-Host "***************************SCRIPT HAS FINISHED***************************"
Write-Host "***************************SCRIPT HAS FINISHED***************************"
Write-Host "***************************SCRIPT HAS FINISHED***************************"

您的 CSV 将包含以下标题:

GivenName LastName LoginName Description Password Manager TargetOU

【讨论】:

  • 嗨,扎克,非常感谢您在这里帮助我。我已经尝试了您提供给我的脚本,当我尝试运行 ps1 文件时,我没有看到这里发生任何事情。尝试查找日志文件,但没有看到生成的日志文件,同时我没有看到任何错误消息。 PS C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell> .\newadusers.ps1 PS C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell>
  • 我昨天打开了一个类似的帖子。请查看stackoverflow.com/questions/44856822/…“1”
  • @Karthik,如果您使用上面的脚本,它只会找到位于 PS1 脚本所在路径内的 CSV。日志将在同一个地方创建。您需要替换上述代码的这一部分以提供 CSV 位置。 $newpath = $path + ".\import_create_ad_users.csv"
  • 我有相关的问题,虽然我是初学者。 ....如果有子域(childa,abc.com childb.abc.com,childc.abc.com)并且包含相同的samaccountname(123456789)AD将如何进行身份验证?是否会一一检查子域以便对用户进行身份验证?我的应用程序仅使用 samaccountname 作为登录 ID。每个子域中这个 samaccount 的密码是什么不同... ??
【解决方案3】:

您需要一次迭代每个项目而不是整个数组:

$samAccountName = $csvcontent | % {
($_.GivenName.Substring(0,1))+($_.LastName)
}

【讨论】:

    猜你喜欢
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多