简体   繁体   English

使用 powershell 从 CSV 导入 AD 属性

[英]Importing AD attributes from CSV using powershell

Im new to PowerShell and I'm attempting to update AD attributes using PowerShell from a.csv file我是 PowerShell 的新手,我正在尝试使用来自 a.csv 文件的 PowerShell 更新 AD 属性

below are the rows in my csv file and what attributes I'm trying to update下面是我的 csv 文件中的行以及我要更新的属性

Department > Department部门 > 部门

Division > Division事业部 > 事业部

Service > info服务 > 信息

EmployeeFullname (using to identify the object) EmployeeFullname(用于标识对象)

LineMangerFullname > manager LineManger全名 > 经理

CostCentre > departmentNumber成本中心 > 部门编号

JobTitle > title职称 > 职称

So far i've only been able to update the Department, Division, Title (Job Title) & Manager attributes in Active Directory到目前为止,我只能更新 Active Directory 中的部门、部门、职务(职务)和经理属性

I am using the script below which updates these attributes successfully我正在使用下面的脚本成功更新这些属性

$Users = Import-CSV C:\Users\user\Documents\CurrentWork\userlist.csv

ForEach ($User in $Users) {
    Get-ADUser -Filter * -property displayname | where {$_.displayname -eq $User.EmployeeFullName} | Set-ADUser -department $User.Department -Division $User.Division -Title $User.JobTitle -Manager $User.LineMangerFullname
}

however when adding info and departmentNumber to the script (below), it fails with:但是,将infodepartmentNumber添加到脚本(如下)时,失败并显示:

 "parameter name 'info' is ambiguous" and A parameter cannot be found that matches parameter name 'departmentNumber'
$Users = Import-CSV C:\Users\user\Documents\CurrentWork\userlist.csv

ForEach ($User in $Users) {
    Get-ADUser -Filter * -property displayname | where {$_.displayname -eq $User.EmployeeFullName} | Set-ADUser -department $User.Department -Division $User.Division -Title $User.JobTitle -Manager $User.LineMangerFullname -info $User.Service -departmentNumber $User.'Cost Centre'
}

does anyone know what im doing wrong or how I can get these to update please and also how i can export the results to see if the update is successful?有谁知道我做错了什么或如何让这些更新以及如何导出结果以查看更新是否成功? really stuck here真的卡在这里

Set-ADUser does not expose all possible AD schema attributes as parameters, only a limited set of common user attributes - and the info attributes (or "Notes" as it's displayed in some tools) is not one of the ones it has parameters for. Set-ADUser不会将所有可能的 AD 架构属性公开为参数,仅公开一组有限的常见用户属性 - 并且info属性(或在某些工具中显示的“注释”)不是它具有参数的属性之一。

To set the value of a property that doesn't have a corresponding parameter, use the -Replace or -Add parameter(s), by passing a hashtable that describes the update:要设置没有相应参数的属性的值,请使用-Replace-Add参数,通过传递描述更新的哈希表:

... |Set-ADUser -Replace @{ info = "new info value" }

As commented, The Manager property can only be one of DistinguishedName, GUID, SID or SamAccountName for Set-AccountName.正如所评论的,对于 Set-AccountName, Manager属性只能是DistinguishedName, GUID, SID or SamAccountName之一。 Apparently, the CSV does not contain any of that and $User.LineMangerFullname contains the full First and Lastname of the manager.显然, CSV 不包含任何这些,并且$User.LineMangerFullname包含经理的完整名字和姓氏。

For that, you need to use another Get-ADUser call to retrieve a valid property from that manager.为此,您需要使用另一个Get-ADUser调用从该管理器检索有效属性。

Next, as Mathias R.接下来,作为 Mathias R。 Jessen already pointed out in his helpful answer , not all attributes are directly available in the parameters of Set-ADUser, so for some you will have to use -Replace @{property = value} syntax. Jessen 已经在他的有用答案中指出,并非所有属性都可以直接在 Set-ADUser 的参数中使用,因此对于某些您将不得不使用-Replace @{property = value}语法。

Finally, I'd recommend using Splatting on cmdlets that potentially can take a lot of parameters to make the code clean/maintainable.最后,我建议在 cmdlet 上使用Splatting ,它可能需要很多参数来使代码干净/可维护。

Try尝试

$Users = Import-CSV C:\Users\user\Documents\CurrentWork\userlist.csv

foreach ($User in $Users) {
    $adUser = Get-ADUser -Filter "DisplayName -eq '$($User.EmployeeFullName)'" -Properties DisplayName -ErrorAction SilentlyContinue
    if ($adUser) {
        # set up a Splatting Hashtable for the Set-ADUser cmdlet
        $userParams = @{
            Department = $User.Department
            Division   = $User.Division
            Title      = $User.JobTitle
        }
        $manager = Get-ADUser -Filter "Name -eq "$($User.LineMangerFullname) -ErrorAction SilentlyContinue
        # if we did find a manager, add property 'Manager' to the Hashtable
        if ($manager) { $userParams['Manager'] = $manager.DistinguishedName }

        # use the Hash to splat all known parameters to Set-ADUser
        $adUser | Set-ADUser $userParams
        # next , update the attributes that have no direct parameter in Set-ADUser
        $adUser | Set-ADUser -Replace @{ info = $User.Service; departmentNumber = $User.'Cost Centre' }
    }
    else {
        Write-Warning "User $($User.EmployeeFullName) could not be found"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM