【问题标题】:PowerShell Active Directory Loop Through All User PropertiesPowerShell Active Directory 循环遍历所有用户属性
【发布时间】:2015-11-29 04:02:12
【问题描述】:

我目前正在使用一些 PowerShell 来更新 Active Directory 用户属性。该脚本将从 CSV 读取更新后的属性。

我想要实现的是遍历用户并将每个用户属性与存储在 CSV 中的值进行比较。如果 CSV 属性值与用户的 Active Directory 属性不匹配,我想更新 Active Directory 中的值

目前我可以选择一个用户并使用以下方式显示所有属性:

Get-ADUser -Filter "UserPrincipalName -eq '$($upn)'" -Properties * -SearchBase 'DC=core,DC=com' 

我正在努力的是能够遍历每个用户的所有属性并将它们与该用户的 CSV 值进行比较。

这是我正在使用的 sn-p:

# Import CSV into variable $users
$users = Import-Csv -Path 'C:\PowerShell\AD\UserUpdates.csv'

# Loop through each user
foreach ($user in $users) {

#Search in specified OU and Update existing attributes
$userproperties = Get-ADUser -Filter "UserPrincipalName -eq '$($user.UserPrincpalName)'" -Properties * -SearchBase 'DC=core,DC=com' 

}

有谁知道循环遍历用户的所有用户配置文件属性的方法?

任何帮助或指导将不胜感激?

更新

好吧,再进一步,我已经取得了进展,但我认为这不是实现这一目标的最干净的方式。

 $userproperties = Get-ADUser -Filter "UserPrincipalName -eq '$($upn)'" -Properties * -SearchBase 'DC=core,DC=com' | Select-Object Name,Created, LastLogon,GivenName,SurName,DisplayName,DistinguishedName,UserPrincipleName

这允许我选择以下项目:

$userproperties.DisplayName

但是使用这种方法,我需要列出我希望使用的每个属性。我希望能够遍历所有属性。也许我可以将我希望使用的所有属性放入一个数组并循环遍历它?

【问题讨论】:

    标签: powershell csv active-directory


    【解决方案1】:

    这是一种循环进入对象属性的方法(在这种情况下是 AD 用户):

    $user = Get-ADUser -Filter "UserPrincipalName -eq '$($user.UserPrincpalName)'" -Properties * -SearchBase 'DC=core,DC=com'
    
    $user | gm | ? membertype -eq property | select -expa name | % { $user.$_ }
    

    foreach-object (%) 中可以添加更新某些属性所需的逻辑

    【讨论】:

      【解决方案2】:

      循环遍历 CSV 文件中一个条目的所有属性并不难。诀窍是转换通过循环导入的哈希表 将csv数据转换成PS对象,如下:

      # Import CSV into variable $users
      $users = Import-Csv -Path 'C:\PowerShell\AD\UserUpdates.csv'
      
      # Loop through each user
      foreach ($user in $users) {
      
      #Obtain attributes from corresponding ADuser.
      $userproperties = Get-ADUser -Filter '
         "UserPrincipalName -eq '$($user.UserPrincpalName)'" `
         -Properties * -SearchBase 'DC=core,DC=com' 
      
      
      
      #Search in specified OU and Update existing attributes
      
         foreach ($prop in $user.psobject.properties) {
            Set-variable -name $prop.name -value $prop.value
      # Instead of doing a set-variable, you could set the corresponding attribute
      # in the appropriate AD.
            }
      
      
      }
      

      【讨论】:

        【解决方案3】:

        Set-ADUser 有一个-Replace 参数,它接受属性和值的哈希表,您可以使用这些哈希表一次更新多个属性。无需为每个用户循环遍历每个属性,您只需构建该哈希表,然后执行单个更新操作。您可以通过从 CSV 中返回您正在检查的 AD 用户属性来提高效率。只需从导入的 CSV 文件创建的集合中的第一个对象获取属性列表,即可获得该属性列表。

        # Import CSV into variable $users
        $CSVusers = Import-Csv -Path 'C:\PowerShell\AD\UserUpdates.csv'
        
        #Get the list of properties to check
        $Properties = $CSVusers[0].psobject.properties.name
        
        # Loop through each user
        foreach ($CSVuser in $CSVusers) {
        
        $UpdateProperties = @{}
        
        #Search in specified OU and Update existing attributes
        $ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$($CSVuser.UserPrincpalName)'" -Properties $Properties -SearchBase 'DC=core,DC=com' 
        
        #Create a hash table of properties that need updated
          Foreach ($Property in $Properties)
           { 
             if ($CSVUser.$Property -ne $ADUser.$Property)
               { $UpdateProperties[$Property] = $CSVuser.$Property }
           }
        
         #Update user
        
         if ( $UpdateProperties.Count -gt 0 )
           { Set-ADUser $ADUser.DistinguishedName -Replace $UpdateProperties }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2023-03-26
          • 1970-01-01
          • 2015-12-29
          • 2015-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-09
          相关资源
          最近更新 更多