[英]PowerShell Add users to AD group via CSV - script not adding users
> # Start transcript Start-Transcript -Path C:\Temp\Add-ADUsers.log -Append
>
> # Import AD Module Import-Module ActiveDirectory
>
> # Import the data from CSV file and assign it to variable $Users = Import-Csv "C:\Temp\jacktest.csv"
>
> # Specify target group where the users will be added to
> # You can add the distinguishedName of the group. For example: CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local $Group = "JackTest"
>
> foreach ($User in $Users) {
> # Retrieve UPN
> $UPN = $User.UserPrincipalName
>
> # Retrieve UPN related SamAccountName
> $ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$UPN'" | Select-Object SamAccountName
>
> # User from CSV not in AD
> if ($ADUser -eq $null) {
> Write-Host "$UPN does not exist in AD" -ForegroundColor Red
> }
> else {
> # Retrieve AD user group membership
> $ExistingGroups = Get-ADPrincipalGroupMembership $ADUser.SamAccountName | Select-Object Name
>
> # User already member of group
> if ($ExistingGroups.Name -eq $Group) {
> Write-Host "$UPN already exists in $Group" -ForeGroundColor Yellow
> }
> else {
> # Add user to group
> Add-ADGroupMember -Identity $Group -Members $ADUser.SamAccountName -WhatIf
> Write-Host "Added $UPN to $Group" -ForeGroundColor Green
> }
> } } Stop-Transcript
Code not add users to group successfully I am trying to add 900+ users to an AD group from CSV with a heading "UserPrincipalName" The reporting else if statements are working as expected.代码未成功将用户添加到组我正在尝试将 900 多个用户添加到来自 CSV 的 AD 组,标题为“UserPrincipalName”报告 else if 语句按预期工作。
I think your code is good enough and the reason for no changes made I believe is the -WhatIf
switch, that should display a message and not do the action.我认为您的代码足够好,我认为没有进行任何更改的原因是
-WhatIf
开关,它应该显示一条消息而不是执行操作。
That aside, there are a couple of things you can consider, one is the | Select-object
除此之外,您可以考虑几件事,其中之一是
| Select-object
| Select-object
this would modify the object to PSCustomObject
and you would loose all the methods and benefits of having an ADObject
. | Select-object
这会将 object 修改为PSCustomObject
,您将失去拥有ADObject
的所有方法和好处。 Another thing would be the comparison you use, instead of -eq
againsta list you better use -contains
, so you get true/false.另一件事是您使用的比较,而不是
-eq
与列表的比较,您最好使用-contains
,因此您得到真/假。 Third but not least is the comparison to $null
, in that particular case I think you dont really need that, but you could rather just see if something is returned with (-not $ADUser)
第三但并非最不重要的是与
$null
的比较,在这种特殊情况下,我认为您并不真正需要它,但是您可以只查看是否返回了某些内容(-not $ADUser)
With all that in mind, I have modify the code to my comments.考虑到所有这些,我将代码修改为我的评论。
foreach ($User in $Users) {
# Retrieve UPN
$UPN = $User.UserPrincipalName
# Retrieve UPN related SamAccountName
$ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$UPN'"
# User from CSV not in AD
if (-not $ADUser) {
Write-Host "$UPN does not exist in AD" -ForegroundColor Red
}
else {
# Retrieve AD user group membership
$ExistingGroups = Get-ADPrincipalGroupMembership $ADUser.SamAccountName
# User already member of group
if ($ExistingGroups.Name -contains $Group) {
Write-Host "$UPN already exists in $Group" -ForeGroundColor Yellow
}
else {
# Add user to group
Add-ADGroupMember -Identity $Group -Members $ADUser.SamAccountName
Write-Host "Added $UPN to $Group" -ForeGroundColor Green
}
}
} Stop-Transcript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.