简体   繁体   English

更新 AD Powershell 中的属性

[英]Updating Attributes in AD Powershell

I have a script updating attributes in AD.我在 AD 中有一个脚本更新属性。 Attached is a sample of my script to update the attributes.附件是我更新属性的脚本示例。 This does work but I am not a PowerShell guy by any means.这确实有效,但无论如何我都不是 PowerShell 的人。 I was wondering if there is a more efficient way to run my script.我想知道是否有更有效的方法来运行我的脚本。 Some issues I am having is if a field in the CSV is blank it generates an error, and if there is blank field on csv file it does not replace existing data in the attribute.我遇到的一些问题是,如果 CSV 中的字段为空,则会生成错误,如果 csv 文件中有空白字段,则不会替换属性中的现有数据。

Import-Module ActiveDirectory
$users = Import-Csv -Path "C:\sdk\employees to update.csv"

#####Udating givenName
foreach ($user in $users) {
    Get-ADUser -Filter "FAKEcompanyEmployeeNumber -eq '$($user.EMPLOYEE)'" -Properties givenName -SearchBase "OU=Staff Win10,dc=FAKEcompany,dc=on,dc=ca" | 
    Set-ADUser -replace @{"givenName" = "$($user.Emp_First_Name)" }
}

#####Udating sn
foreach ($user in $users) {
    Get-ADUser -Filter "FAKEcompanyEmployeeNumber -eq '$($user.EMPLOYEE)'" -Properties sn -SearchBase "OU=Staff Win10,dc=FAKEcompany,dc=on,dc=ca" | 
    Set-ADUser -replace @{"sn" = "$($user.Emp_Last_Name)" }
}

#####Udating FAKEcompanyEmployeeNumber
foreach ($user in $users) {
    Get-ADUser -Filter "FAKEcompanyEmployeeNumber -eq '$($user.EMPLOYEE)'" -Properties FAKEcompanyEmployeeNumber -SearchBase "OU=Staff Win10,dc=FAKEcompany,dc=on,dc=ca" | 
    Set-ADUser -replace @{"FAKEcompanyEmployeeNumber" = "$($user.Employee)" }
}

#####Udating middleName
foreach ($user in $users) {
    Get-ADUser -Filter "FAKEcompanyEmployeeNumber -eq '$($user.EMPLOYEE)'" -Properties middleName -SearchBase "OU=Staff Win10,dc=FAKEcompany,dc=on,dc=ca" | 
    Set-ADUser -replace @{"middleName" = "$($user.Middle_Name)" }
}
#The where filter on $_.[propertyname] filters objects out where the specified properties are empty 
$users = Import-Csv -Path "C:\sdk\employees to update.csv" | ?{$_.employee -and $_.Emp_First_Name -and $_.Middle_Name}

#As Santiago mentioned you can do it in one step
foreach ($user in $users){
    Get-ADUser -Filter "FAKEcompanyEmployeeNumber -eq '$($user.EMPLOYEE)'" -Properties givenName -SearchBase "OU=Staff Win10,dc=FAKEcompany,dc=on,dc=ca" | set-aduser -replace @{givenName=$user.Emp_First_Name;sn=$user.Emp_Last_Name;FAKEcompanyEmployeeNumber=$user.Employee;middleName=$user.Middle_Name}
}

#################

#If you want to process also objects with partital information you can do
$users = Import-Csv -Path "C:\sdk\employees to update.csv"
foreach ($user in $users){
    $Replace = @{}
    If ($user.Emp_First_Name){
        $replace.add('givenName',$user.Emp_First_Name)
    }
    If ($user.Emp_Last_Name){
        $replace.add('sn',$user.Emp_Last_Name)
    }
    If ($user.Employee){
        $replace.add('FAKEcompanyEmployeeNumber',$user.Employee)
    }
    If ($user.middle_name){
        $replace.add('middleName',$user.middle_name)
    }
    Get-ADUser -Filter "FAKEcompanyEmployeeNumber -eq '$($user.EMPLOYEE)'" -Properties givenName -SearchBase "OU=Staff Win10,dc=FAKEcompany,dc=on,dc=ca" | set-aduser -replace $replace
}

You can set multiple replacement values for user attributes as shown in the Set-ADUser documentation in Example 3 .您可以为用户属性设置多个替换值,如示例 3中的Set-ADUser文档所示。

String.IsNullOrWhiteSpace can help you check if the value for the column is empty in your Csv. String.IsNullOrWhiteSpace可以帮助您检查您的 Csv 中该列的值是否为空。

Hopefully the inline comments can help you understand the logic.希望内联注释可以帮助您理解其中的逻辑。

$properties = @{
    givenName  = 'Emp_First_Name'
    sn         = 'Emp_Last_Name'
    middleName = 'Middle_Name'
    FAKEcompanyEmployeeNumber = 'Employee'
}

Import-Csv -Path "C:\sdk\employees to update.csv" | ForEach-Object {
    $params = @{
        Filter     = "FAKEcompanyEmployeeNumber -eq '{0}'" -f $_.EMPLOYEE
        SearchBase = 'OU=Staff Win10,dc=FAKEcompany,dc=on,dc=ca'
    }
    # if we can find the user in AD,
    # check which properties need to be updated for this user
    if($adUser = Get-ADUser @params) {
        # replace hashtable for splatting latter
        $replace = @{ Replace = @{} }
        foreach($property in $properties.GetEnumerator()) {
            $value = $_.($property.Value)
            # if this Value is empty in the Csv
            if([string]::IsNullOrWhiteSpace($value)) {
                # skip it, go next
                continue
            }
            # here we assume the Value in the CSV is populated,
            # add it to the replacement hash
            $replace['Replace'][$property.Key] = $value
        }

        # check one last time if the replacement hash is populated
        if($replace.Values.Keys.Count) {
            # if it is, we can go ahead and set values for the AD User
            $adUser | Set-ADUser @replace
        }
    }
}

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

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