[英]Powershell Active Directory DisplayName
I have a powershell script that builds active directory users based on CSV file.我有一个基于 CSV 文件构建活动目录用户的 powershell 脚本。 It has the attributes defined like this:
它具有如下定义的属性:
-GivenName $_.FirstName `
-Surname $_.LastName `
-SamAccountName $_.UserName `
I want to add the DisplayName attribute to combine $ .FirstName and $ .LastName with a space in the between.我想添加 DisplayName 属性以将 $ .FirstName 和 $ .LastName 与中间的空格组合在一起。 I tried:
我试过:
-DisplayName $_.FirstName + " " + $_.LastName `
But the above doesn't work, it gives an error.但是上面的不起作用,它给出了错误。
Can anyone kindly suggest how I can define the DisplayName attribute with the firstname and lastname from the CSV data?任何人都可以建议我如何使用 CSV 数据中的名字和姓氏定义 DisplayName 属性吗?
Thanks谢谢
When PowerShell looks at arguments passed to a command, each individual token is interpreted as a separate input argument.当PowerShell 查看传递给命令的参数时,每个单独的标记都被解释为单独的输入参数。
Wrap the string concatenation in a sub-expression ( $()
) to pass it as a single string:将字符串连接包装在子表达式 (
$()
) 中以将其作为单个字符串传递:
Set-ADUser ... -DisplayName $($_.FirstName + " " + $_.LastName)
or use an expandable string:或使用可扩展的字符串:
Set-ADUser ... -DisplayName "$($_.FirstName) $($_.LastName)"
As Lee_Dailey notes , you might want to use a technique called "splatting" to organize your parameter arguments instead of splitting them over multiple lines:正如Lee_Dailey 指出的那样,您可能希望使用一种称为“splatting”的技术来组织您的参数参数,而不是将它们拆分为多行:
Import-Csv users.csv |ForEach-Object {
$parameterArgs = @{
Name = $_.Name
SamAccountName = $_.UserName
GivenName = $_.FirstName
Surname = $_.LastName
DisplayName = $_.FirstName,$_.LastName -join ' '
}
# pass our parameter arguments by "@splatting"
New-ADUser @parameterArgs
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.