Import-CSV "C:\Temp\jacktest.csv" | Foreach-Object {
$aduser = Get-ADUser -Filter "UPN-eq '$($_.UPN)'"
if( $aduser ) {
Write-Output "Adding user $($aduser.SamAccountName) to groupname"
Add-ADGroupMember -Identity JackTest -Members $aduser
} else {
Write-Warning "Could not find user in AD with email address $($_.EmailAddress)"
}
}
我收到以下错误:
脚本已启动,输出档案为 C:\Temp\Add-ADUsers.log Get-ADUser :无法识别搜索过滤器 At line:19 char:15
-
$ADUser = Get-ADUser -Filter "UPN -eq '$UPN'" | Select-Object Sam ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
- FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
uj5u.com热心网友回复:
此答案旨在帮助您解决问题,以便我们了解您的 CSV 可能出现的问题。
请注意,此代码假定您的 CSV 是逗号分隔的,并且 CSV 有一个名为“UserPrincipalName”的列。
$usersToAdd = foreach($line in Import-CSV "C:\Temp\jacktest.csv")
{
if([string]::IsNullOrWhiteSpace($line.UserPrincipalName))
{
Write-Warning 'Empty UserPrincialName Value:'
Write-Warning $line
continue
}
$aduser = Get-ADUser -Filter "UserPrincipalName -eq '$($line.UserPrincipalName)'"
if(-not $aduser)
{
Write-Warning "$($line.UserPrincipalName) could not be found."
continue
}
$aduser
}
if($usersToAdd)
{
Write-Host 'The following users will be added to the Group'
$usersToAdd.UserPrincialName
try
{
Add-ADGroupMember -Identity JackTest -Members $usersToAdd
}
catch
{
Write-Warning $_.Exception.Message
}
}
uj5u.com热心网友回复:
检查 csv 档案,您似乎没有使用默认分隔符。如果是这样,请将自变量 -delimiter 添加到 import-csv cmdlet。例如对于制表符分隔符:
Import-CSV "C:\Temp\jacktest.csv" -delimiter "`t"
0 评论