【问题标题】:Powershell - rename users (loop)Powershell - 重命名用户(循环)
【发布时间】:2018-04-22 22:26:29
【问题描述】:

我正在尝试更新我域中的几个 (560) 用户。 根据我的工人数据库,他们使用的名称不完整和/或不正确。

我创建了一个包含此信息的 CSV 文件:

samaccoutname,Name,givenname,surname
r001248,ADRIANA DAS COUVE ,ADRIANA ,DAS COUVE
r020230,ALEXANDRA DAS NEVE ,ALEXANDRA ,DAS NEVE

这是我的代码,但它不起作用:

#
# Script.ps1
#

Import-Module activedirectory

$userlist = Import-Csv C:\Users\r013462\Documents\Atualização_AD.csv -Delimiter ","

foreach ($user in $userlist)
{
    $GivenN = $user.givenName
    $FullN = $user.Name
    $SurN = $user.surName
    Get-ADUser -Identity $user.samaccountname | Set-ADUser -GivenName $GivenN -Surname $SurN -DisplayName $FullN
}

建议?

【问题讨论】:

  • 您的子表达式 $() 完全没有必要。
  • 已更改;到,以及这些更改,仍然无法正常工作 foreach ($userlist 中的$user) { $GivenN = $user.givenName $FullN = $user.Name $SurN = $user.surName Get-ADUser -Identity $user.samaccountname | Set-ADUser -GivenName $GivenN -Surname $SurN -DisplayName $FullN }
  • 添加了 -delimiter "," 无效
  • 将此作为返回:Get-ADUser:无法验证参数“身份”上的参数。参数为空。为参数提供一个有效值,然后再次尝试运行该命令。 [错误] 在 C:\Users\r013462\source\repos\rename-project\rename-project\Script.ps1:14 char:23 [错误] + Get-ADUser -Identity $user.samaccountname |设置-ADUser -GivenName ...
  • 缺少大写字符。 samaccoutname,Name,givenname,surname -> samaccouNtname,Name,givenname,surname

标签: powershell active-directory ldap rename bulk-rename-utility


【解决方案1】:

好的,终于找到解决办法了!

如果你想这样做,你必须使用我最初发布的 csv 的相同参数并使用这个脚本:

#
# Script.ps1
#

Import-Module activedirectory

$varCSV = ""
$userlist = Import-Csv -Path $varCSV -Delimiter ","

foreach ($user in $userlist)
{
    $samN = $user.samaccouNtname
    $GivenN = $user.GivenName
    $FullN = $user.Name
    $SurN = $user.Surname
    $dn = (Get-ADUser -Identity $samN).DistinguishedName
    Get-ADUser -Identity $user.SamAccountName | Set-ADUser -GivenName $GivenN -SurName $SurN -DisplayName $FullN  
    Try {
        Rename-ADObject $dn -NewName $FullN
    }

    catch {
        Write-Output "usuario repetido: " ($user.samaccountname) | Out-File C:\errors.txt -Append
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 2021-07-21
    • 2021-12-01
    • 1970-01-01
    • 2019-07-05
    • 2021-09-14
    相关资源
    最近更新 更多