简体   繁体   English

使用 PowerShell 从.csv 更新 Active Directory 管理器属性

[英]Updating Active Directory Manager Attribute from .csv Using PowerShell

I'm not so good at PowerShell.我不太擅长 PowerShell。 I have this script which worked before that I've have used to update the manager attribute in Active Directory, however, now after just replacing the file its no longer working我有这个脚本,在此之前我已经用来更新 Active Directory 中的管理器属性,但是,现在在替换文件之后它不再工作

 $Users = Import-CSV C:\Documents\Managers.csv ForEach ($User in $Users) { $user= Get-ADUser -Filter "displayname -eq '$($Users.EmployeeFullName)'"|select -ExpandProperty samaccountname $manager=Get-ADUser -Filter "displayname -eq '$($Users.'Line Manager Fullname')'"|select -ExpandProperty DistinguishedName Set-ADUser -Identity $user -Replace @{manager=$manager} }

when running the script it returns:运行脚本时它返回:

 Set-ADUser: Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again. At line:6 char:22 + Set-ADUser -Identity $user -Replace @{manager=$manager} + ~~~~~ + CategoryInfo: InvalidData: (:) [Set-ADUser], ParentContainsErrorRecordException + FullyQualifiedErrorId: ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser

the "EmployeeFullName" and "Line Manager FullName" format in the.csv is set as ie joe bloggs, see below CSV file example.将.csv中的“EmployeeFullName”和“Line Manager FullName”格式设置为ie joe bloggs,见下面CSV文件示例。

EmployeeFullname   Line Manager Fullname 
----------------   ---------------------
Kieran Rhodes      Tracey Barley    
Lewis Pontin       Tracey Barley
Lizzy Wilks        Rodney Bennett

I also noticed if I remove and try to retype "$Users.EmployeeFullName" it no longer picks up the "EmployeeFullName" from the csv file.我还注意到,如果我删除并尝试重新输入“$Users.EmployeeFullName”,它不再从 csv 文件中获取“EmployeeFullName”。

Anyone know what I'm doing wrong here please?有人知道我在这里做错了什么吗?

Below would be an example:下面是一个例子:

$Users = Import-CSV C:\Documents\Managers.csv
ForEach ($User in $Users) {
   $user    = Get-ADUser -Filter "displayname -eq '$($User.EmployeeFullName)'"
   $manager = (Get-ADUser -Filter "displayname -eq '$($User.'Line Manager Fullname')'").DistinguishedName

Set-ADUser -Identity $User -Replace @{manager=$manager}
}

Note: you don't need to dig down to the samAccountName property because Set-ADUser will take the full fidelity object for the -Identity argument.注意:您不需要深入研究 samAccountName 属性,因为Set-ADUser将为-Identity参数采用完全保真度 object。 You also don't need to use Select... -ExpandProperty you can just parenthetically dot reference the distinguishedName property.您也不需要使用Select... -ExpandProperty您可以在括号中引用 distinctName 属性。

Also you can use the instancing capability in the AD cmdlets:您还可以使用 AD cmdlet 中的实例化功能:

$Users = Import-CSV C:\Documents\Managers.csv
ForEach ( $User in $Users )
{
   $User    = Get-ADUser -Filter "displayname -eq '$($User.EmployeeFullName)'" -Properties Manager
   $Manager = (Get-ADUser -Filter "displayname -eq '$($User.'Line Manager Fullname')'").DistinguishedName

   $User.Manager = $Manager
   Set-ADUser -Instance $User 
}

In this case you call back the Manager property set it with a simple assignment statement than give the $User variable as the argument to the -Instance parameter of Set-ADUser在这种情况下,您使用简单的赋值语句回调 Manager 属性设置它,而不是将$User变量作为Set-ADUser-Instance参数的参数

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

相关问题 使用 Powershell 更新 csv 记录中的活动目录 DN - Updating Active directory DN in csv records with Powershell 使用Powershell更新Active Directory中的Active Directory用户属性 - Updating Active Directory user properties in Active Directory using Powershell 从 Csv 更新 Active Directory 字段 - Updating Active Directory Field from Csv 如何使用 powerShell 在 Active Directory 上填充 manager 字段 - How to populate the manager field on Active Directory, using powerShell 通过 PowerShell 更新 Active Directory - Updating Active Directory via PowerShell 通过Powershell使用导入的.csv从Active Directory中提取名字和姓氏 - Extract First and Last Name from Active Directory using imported .csv through Powershell 如何使用PowerShell将用户从CSV文件添加到Active Directory(AD)和Exchange中? - How do you add users from a CSV file to Active Directory (AD) and Exchange using PowerShell? 如何使用Powershell从csv文件更新Active Directory中的更多数据行? - How can I update more data rows in Active Directory from a csv file using Powershell? 使用CSV将Powershell Active Directory用户分配到多个组 - Powershell Active Directory Users to Multiple Groups using CSV 使用来自csv的powershell删除活动目录samaccount名称中的空白区域 - remove white space in active directory samaccount name with powershell from csv
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM