PowerShell Active Directory 循环遍历所有用户属性

问题描述 投票:0回答:3

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

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

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

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

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

这是我正在使用的片段:

# 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
3个回答
0
投票

这是一种循环进入对象属性的方法(在本例中为 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
%
)你可以添加你需要更新一些proeprty

的逻辑

0
投票

遍历 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.
      }


}

0
投票

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 }

}
© www.soinside.com 2019 - 2024. All rights reserved.