我针对csv文件运行以下PS命令,该文件包含AD组列表,后跟ADUsers。
只要填充了所有字段,脚本就会正常运行,是否有一个命令/开关会忽略非值?
import-csv c:\admin\powershell\ADGroupMembers.csv | foreach {add-adgroupmember -Identity $_.ADGroup $_.member1,$_.member2,$_.member3,$_.member4,$_.member5}
CSV文件内容
ADGroup,Member1,Member2,Member3,Member4,Member5
AD-Test1,Minnie,Mickey,Donald,Daisy,Goofy
AD-Test2,Minnie,Mickey,Donald,,
AD-Test3,Minnie,Mickey,Donald,Pete,Spike
答案 0 :(得分:1)
检查其中一个成员*属性是否没有值:
Import-Csv c:\admin\powershell\ADGroupMembers.csv | Foreach-Object {
$values = $_.PSObject.Properties | Where-Object {$_.Name -clike "Member*" -and [string]::IsNullOrEmpty($_.Value) }
if($values)
{
Write-Warning 'missing values'
}
else
{
Add-ADGroupMember -Identity $_.ADGroup $_.member1,$_.member2,$_.member3,$_.member4,$_.member5
}
}
答案 1 :(得分:0)
我假设您还想添加包含缺失值的行的成员(例如,您要将Minnie,Mickey和Donald添加到AD-Test2)。在这种情况下,我会将我们的csv文件雕刻成具有两个属性的对象集合:ADGroup(字符串)和成员(字符串[])。然后我会把这个集合提供给add-groupmember。
该集合看起来像这样:
<#
ADGroup Members
------- -------
AD-Test1 {Minnie, Mickey, Donald, Daisy...}
AD-Test2 {Minnie, Mickey, Donald}
AD-Test3 {Minnie, Mickey, Donald, Pete...}
#>
如何创建此集合:
# collection creation
$groupmembers = `
import-csv .\ADGroupMembers.csv |
select AdGroup, @{
Name = "Members"
Expression = {
$members = @()
$_.psobject.properties |
Where-Object {$_.Name -clike "Member*" -and (-not [string]::IsNullOrEmpty($_.Value))} |
Foreach-Object {$members += $_.value}
$members
}
}
将集合提供给add-adgroupmember:
$groupmembers | %{Add-ADGroupMember -Identity $_.AdGroup -Members $_.members}