Powershell Active Directory 显示名称 [英] Powershell Active Directory DisplayName
问题描述
我有一个基于 CSV 文件构建活动目录用户的 powershell 脚本.它具有如下定义的属性:
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 查看 传递给命令的参数,每个单独的标记被解释为单独的输入参数.
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)
或使用可扩展的字符串:
or use an expandable string:
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 显示名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!