[英]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.