Powershell Active Directory DisplayName [英] Powershell Active Directory DisplayName
问题描述
我有一个Powershell脚本,可以基于CSV文件构建活动目录用户.它具有如下定义的属性:
I have a powershell script that builds active directory users based on CSV file. It has the attributes defined like this:
-GivenName $_.FirstName `
-Surname $_.LastName `
-SamAccountName $_.UserName `
我想添加DisplayName属性,以将$ .FirstName和$ .LastName结合在一起,并在两者之间留一个空格.我试过了:
I want to add the DisplayName attribute to combine $.FirstName and $.LastName with a space in the between. I tried:
-DisplayName $_.FirstName + " " + $_.LastName `
但是上面的方法不起作用,它给出了一个错误.
But the above doesn't work, it gives an error.
有人可以建议我如何用CSV数据中的名字和姓氏定义DisplayName属性吗?
Can anyone kindly suggest how I can define the DisplayName attribute with the firstname and lastname from the CSV data?
谢谢
推荐答案
When PowerShell looks at arguments passed to a command, each individual token is interpreted as a separate input argument.
将字符串串联包含在子表达式( $()
)中,以将其作为单个字符串传递:
Wrap the string concatenation in a sub-expression ($()
) to pass it as a single string:
Set-ADUser ... -DisplayName $($_.FirstName + " " + $_.LastName)
或使用可扩展字符串:
Set-ADUser ... -DisplayName "$($_.FirstName) $($_.LastName)"
Lee_Dailey注释,您可能想使用一种称为"splatting" 来组织您的参数参数而不是将它们分成多行:
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:
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
}
这篇关于Powershell Active Directory DisplayName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!