【问题标题】:How I can read all user in a OU and change the Attributes with Powershell?如何读取 OU 中的所有用户并使用 Powershell 更改属性?
【发布时间】:2016-11-22 10:03:00
【问题描述】:

您好,我有一个关于 powershell 和活动目录的问题。我想更改 ou 中所有用户的属性。

为此,我有这个 ps 脚本:

Clear

############################################### Settings ####################################################################

$ldap_path = '...'  #secret

<# 1. '*' 
   2. 'XXX*'
   3. 'XXX-XXX*' (XXX sind Zahlen)

   Ansonsten den genauen String eingeben
#>

$search_filter = '...' #Filterparameter

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


#Alle AD-User mit Topdesk in Pager einlesen, Unterhalb OU Abteilungen

$Abteilungen = Get-ADOrganizationalUnit -SearchBase $ldap_path -SearchScope Subtree -Filter 'Name -like $search_filter' | sort 


ForEach($Abteilung in $Abteilungen)  
    {    

       $Kostenstelle = $Abteilung.Name

       $Standort     = $Abteilung.City

       IF([string]::IsNullOrWhiteSpace($Standort)) {   $Standort = "Ohne".Trim() }  

       $DN           = $Abteilung.DistinguishedName


  Write-Host "Kostenstelle: " $Kostenstelle

  Write-Host "Standort....: " $Standort

  $searchb = "..."

$Users = Get-ADUser -Filter { pager -Like "topdesk"  -and Enabled -eq $true } -Searchbase "$searchb"

ForEach($User in $Users)  

       {
...# ???
}
...

    }

我只想读取部门中的所有 UserObjects 并将属性 street 更改为 extensionAttribute2 并将 description 更改为 extensionAttribute1。

【问题讨论】:

  • 您的意思是更改属性名称,还是将街道值设置为 extensionAttribute2 ? (另附注:您最好提供英文代码,以方便大家使用)
  • 对不起,我的下一个问题我全部改成英文

标签: powershell active-directory ldap active-directory-group


【解决方案1】:

我认为你可以在一个班轮中做到这一点。

Get-ADUser -Filter * -SearchBase "OU=Finance,OU=UserAccounts,DC=FABRIKAM,DC=COM" -properties StreetAddress,Description | foreach-object {Set-ADUser $_ -ExtensionAttribute2 ($_.StreetAddress) -ExtensionAttribute1 ($_.Description)}

Get-ADUser -Filter * -SearchBase "OU=Finance,OU=UserAccounts,DC=FABRIKAM,DC=COM" -properties StreetAddress,Description | foreach-object {Set-ADUser $_ -add @{ "ExtensionAttribute2"="$_.StreetAddress","ExtensionAttribute1"="$_.Description"}}   

**根据@kage 评论更改了第二个代码示例。

希望这会有所帮助。

谢谢,蒂姆。

【讨论】:

  • 很确定这是不可能的,-ExtensionAttribute1 和 2 不是 Set-ADUser 命令的参数,至少在 2016 年可能不在 2012r2 DC 上,但还没有尝试过。需要使用 -Add 参数设置它,如这篇文章中所述:stackoverflow.com/questions/15807496/… 也不需要 for-each,可以只做管道,然后 Set-ADUser 它会自动拾取身份对象。跨度>
  • 嘿,谢谢...你知道如何在 if else 中检查 scriptPath 属性是否为空
  • 谢谢@Kage,很好。在没有看到您输入的内容的情况下,这就是我的做法。 if($_.scriptpath -ne $null) { write-host "ScriptPath has data" } else { write-host "ScritpPath is empty" } 希望格式化有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
  • 2020-09-03
  • 2020-01-05
  • 2018-12-23
  • 2012-07-30
  • 2022-10-17
  • 2010-10-02
相关资源
最近更新 更多